* Common code for test_pidfile_{contested,inherited}. */
| 125 | * Common code for test_pidfile_{contested,inherited}. |
| 126 | */ |
| 127 | static const char * |
| 128 | common_test_pidfile_child(const char *fn, int parent_open) |
| 129 | { |
| 130 | struct pidfh *pf = NULL; |
| 131 | pid_t other = 0, pid = 0; |
| 132 | int fd[2], serrno, status; |
| 133 | struct kevent event, ke; |
| 134 | char ch; |
| 135 | int kq; |
| 136 | |
| 137 | unlink(fn); |
| 138 | if (pipe(fd) != 0) |
| 139 | return (strerror(errno)); |
| 140 | |
| 141 | if (parent_open) { |
| 142 | pf = pidfile_open(fn, 0600, &other); |
| 143 | if (pf == NULL && other != 0) |
| 144 | return ("pidfile exists and is locked"); |
| 145 | if (pf == NULL) |
| 146 | return (strerror(errno)); |
| 147 | } |
| 148 | |
| 149 | pid = fork(); |
| 150 | if (pid == -1) |
| 151 | return (strerror(errno)); |
| 152 | if (pid == 0) { |
| 153 | // child |
| 154 | close(fd[0]); |
| 155 | signal(SIGINT, signal_handler); |
| 156 | if (!parent_open) { |
| 157 | pf = pidfile_open(fn, 0600, &other); |
| 158 | if (pf == NULL && other != 0) |
| 159 | return ("pidfile exists and is locked"); |
| 160 | if (pf == NULL) |
| 161 | return (strerror(errno)); |
| 162 | } |
| 163 | if (pidfile_write(pf) != 0) { |
| 164 | serrno = errno; |
| 165 | pidfile_close(pf); |
| 166 | unlink(fn); |
| 167 | return (strerror(serrno)); |
| 168 | } |
| 169 | if (pf == NULL) |
| 170 | _exit(1); |
| 171 | if (pidfile_write(pf) != 0) |
| 172 | _exit(2); |
| 173 | kq = kqueue(); |
| 174 | if (kq == -1) |
| 175 | _exit(3); |
| 176 | EV_SET(&ke, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); |
| 177 | /* Attach event to the kqueue. */ |
| 178 | if (kevent(kq, &ke, 1, NULL, 0, NULL) != 0) |
| 179 | _exit(4); |
| 180 | /* Inform the parent we are ready to receive SIGINT */ |
| 181 | if (write(fd[1], "*", 1) != 1) |
| 182 | _exit(5); |
| 183 | /* Wait for SIGINT received */ |
| 184 | if (kevent(kq, NULL, 0, &event, 1, NULL) != 1) |
no test coverage detected