Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Cyril Hrubis chrubis@suse.cz |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <errno.h> |
| 28 | #include <unistd.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <poll.h> |
| 33 | |
| 34 | #include "tst_checkpoint.h" |
| 35 | |
| 36 | void tst_checkpoint_init(const char *file, const int lineno, |
| 37 | struct tst_checkpoint *self) |
| 38 | { |
| 39 | if (!tst_tmpdir_created()) { |
| 40 | tst_brkm(TBROK, NULL, "Checkpoint could be used only in test " |
| 41 | "temporary directory at %s:%d", |
| 42 | file, lineno); |
| 43 | } |
| 44 | |
| 45 | /* default values */ |
| 46 | self->retval = 1; |
| 47 | self->timeout = 5000; |
| 48 | |
| 49 | unlink(TST_CHECKPOINT_FIFO); |
| 50 | |
| 51 | if (mkfifo(TST_CHECKPOINT_FIFO, 0666)) { |
| 52 | tst_brkm(TBROK | TERRNO, NULL, |
| 53 | "Failed to create fifo '%s' at %s:%d", |
| 54 | TST_CHECKPOINT_FIFO, file, lineno); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void tst_checkpoint_parent_wait(const char *file, const int lineno, |
| 59 | void (*cleanup_fn)(void), |
| 60 | struct tst_checkpoint *self) |
| 61 | { |
| 62 | int ret; |
| 63 | char ch; |
| 64 | struct pollfd fd; |
| 65 | |
| 66 | fd.fd = open(TST_CHECKPOINT_FIFO, O_RDONLY | O_NONBLOCK); |
| 67 | |
| 68 | if (fd.fd < 0) { |
| 69 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 70 | "Failed to open fifo '%s' at %s:%d", |
| 71 | TST_CHECKPOINT_FIFO, file, lineno); |
| 72 | } |
| 73 | |
| 74 | fd.events = POLLIN; |
| 75 | fd.revents = 0; |
| 76 | |
| 77 | ret = poll(&fd, 1, self->timeout); |
| 78 | |
| 79 | switch (ret) { |
| 80 | case 0: |
| 81 | close(fd.fd); |
| 82 | tst_brkm(TBROK, cleanup_fn, "Checkpoint timeouted after " |
| 83 | "%u msecs at %s:%d", self->timeout, file, lineno); |
| 84 | break; |
| 85 | case 1: |
| 86 | break; |
| 87 | default: |
| 88 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 89 | "Poll failed for fifo '%s' at %s:%d", |
| 90 | TST_CHECKPOINT_FIFO, file, lineno); |
| 91 | } |
| 92 | |
| 93 | ret = read(fd.fd, &ch, 1); |
| 94 | |
| 95 | switch (ret) { |
| 96 | case 0: |
| 97 | close(fd.fd); |
| 98 | tst_brkm(TBROK, cleanup_fn, |
| 99 | "The other end of the pipe was closed " |
| 100 | "unexpectedly at %s:%d", file, lineno); |
| 101 | break; |
| 102 | case -1: |
| 103 | close(fd.fd); |
| 104 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 105 | "Failed to read from pipe at %s:%d\n", |
| 106 | file, lineno); |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | if (ch != 'c') { |
| 111 | close(fd.fd); |
| 112 | tst_brkm(TBROK, cleanup_fn, |
| 113 | "Wrong data read from the pipe at %s:%d\n", |
| 114 | file, lineno); |
| 115 | } |
| 116 | |
| 117 | close(fd.fd); |
| 118 | } |
| 119 | |
| 120 | void tst_checkpoint_child_wait(const char *file, const int lineno, |
| 121 | struct tst_checkpoint *self) |
| 122 | { |
| 123 | int ret, fd; |
| 124 | char ch; |
| 125 | |
| 126 | fd = open(TST_CHECKPOINT_FIFO, O_RDONLY); |
| 127 | |
| 128 | if (fd < 0) { |
| 129 | fprintf(stderr, "CHILD: Failed to open fifo '%s': %s at " |
| 130 | "%s:%d\n", TST_CHECKPOINT_FIFO, strerror(errno), |
| 131 | file, lineno); |
| 132 | exit(self->retval); |
| 133 | } |
| 134 | |
| 135 | ret = read(fd, &ch, 1); |
| 136 | |
| 137 | if (ret == -1) { |
| 138 | fprintf(stderr, "CHILD: Failed to read from fifo '%s': %s " |
| 139 | "at %s:%d\n", TST_CHECKPOINT_FIFO, strerror(errno), |
| 140 | file, lineno); |
| 141 | goto err; |
| 142 | } |
| 143 | |
| 144 | if (ch != 'p') { |
| 145 | fprintf(stderr, "CHILD: Wrong data read from the pipe " |
| 146 | "at %s:%d\n", file, lineno); |
| 147 | goto err; |
| 148 | } |
| 149 | |
| 150 | close(fd); |
| 151 | return; |
| 152 | err: |
| 153 | close(fd); |
| 154 | exit(self->retval); |
| 155 | } |
| 156 | |
| 157 | void tst_checkpoint_signal_parent(const char *file, const int lineno, |
| 158 | struct tst_checkpoint *self) |
| 159 | { |
| 160 | int ret, fd; |
| 161 | |
| 162 | fd = open(TST_CHECKPOINT_FIFO, O_WRONLY); |
| 163 | |
| 164 | if (fd < 0) { |
| 165 | fprintf(stderr, "CHILD: Failed to open fifo '%s': %s at %s:%d", |
| 166 | TST_CHECKPOINT_FIFO, strerror(errno), file, lineno); |
| 167 | exit(self->retval); |
| 168 | } |
| 169 | |
| 170 | ret = write(fd, "c", 1); |
| 171 | |
| 172 | switch (ret) { |
| 173 | case 0: |
| 174 | fprintf(stderr, "No data written, something is really " |
| 175 | "screewed; at %s:%d\n", file, lineno); |
| 176 | goto err; |
| 177 | break; |
| 178 | case -1: |
| 179 | fprintf(stderr, "Failed to write to pipe: %s at %s:%d\n", |
| 180 | strerror(errno), file, lineno); |
| 181 | goto err; |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | close(fd); |
| 186 | return; |
| 187 | err: |
| 188 | close(fd); |
| 189 | exit(self->retval); |
| 190 | } |
| 191 | |
| 192 | void tst_checkpoint_signal_child(const char *file, const int lineno, |
| 193 | void (*cleanup_fn)(void), |
| 194 | struct tst_checkpoint *self) |
| 195 | { |
| 196 | int ret, fd; |
| 197 | |
| 198 | fd = open(TST_CHECKPOINT_FIFO, O_WRONLY); |
| 199 | |
| 200 | if (fd < 0) { |
| 201 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 202 | "Failed to open fifo '%s' at %s:%d", |
| 203 | TST_CHECKPOINT_FIFO, file, lineno); |
| 204 | } |
| 205 | |
| 206 | ret = write(fd, "p", 1); |
| 207 | |
| 208 | switch (ret) { |
| 209 | case 0: |
| 210 | close(fd); |
| 211 | tst_brkm(TBROK, cleanup_fn, |
| 212 | "No data written, something is really screewed; " |
| 213 | "at %s:%d\n", file, lineno); |
| 214 | break; |
| 215 | case -1: |
| 216 | close(fd); |
| 217 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 218 | "Failed to write to pipe at %s:%d\n", |
| 219 | file, lineno); |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | close(fd); |
| 224 | } |