Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include <sys/types.h> |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 19 | #include <sys/socket.h> |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 20 | #include <signal.h> |
| 21 | #include <poll.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 22 | #include <sys/wait.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <errno.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <libgen.h> |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 29 | #include <stdbool.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 30 | |
| 31 | #include <logwrap/logwrap.h> |
| 32 | #include "private/android_filesystem_config.h" |
| 33 | #include "cutils/log.h" |
| 34 | |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame^] | 35 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 36 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 37 | static int signal_fd_write; |
| 38 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 39 | #define ERROR(fmt, args...) \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 40 | do { \ |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 41 | fprintf(stderr, fmt, ## args); \ |
| 42 | ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 43 | } while(0) |
| 44 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 45 | #define FATAL_CHILD(fmt, args...) \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 46 | do { \ |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 47 | ERROR(fmt, ## args); \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 48 | _exit(-1); \ |
| 49 | } while(0) |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 50 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 51 | static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid, |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 52 | int *chld_sts, bool logwrap) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 53 | int status = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 54 | char buffer[4096]; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 55 | struct pollfd poll_fds[] = { |
| 56 | [0] = { |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 57 | .fd = signal_fd, |
| 58 | .events = POLLIN, |
| 59 | }, |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 60 | [1] = { |
| 61 | .fd = parent_read, |
| 62 | .events = POLLIN, |
| 63 | }, |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 64 | }; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 65 | int rc = 0; |
| 66 | sigset_t chldset; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 67 | |
| 68 | int a = 0; // start index of unprocessed data |
| 69 | int b = 0; // end index of unprocessed data |
| 70 | int sz; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 71 | bool remote_hung = false; |
| 72 | bool found_child = false; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 73 | |
| 74 | char *btag = basename(tag); |
| 75 | if (!btag) btag = (char*) tag; |
| 76 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 77 | sigemptyset(&chldset); |
| 78 | sigaddset(&chldset, SIGCHLD); |
Rom Lemarchand | ed179d2 | 2013-01-16 15:07:30 -0800 | [diff] [blame] | 79 | pthread_sigmask(SIG_UNBLOCK, &chldset, NULL); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 80 | |
| 81 | while (!found_child) { |
| 82 | if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) { |
| 83 | if (errno == EINTR) |
| 84 | continue; |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 85 | ERROR("poll failed\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 86 | rc = -1; |
| 87 | goto err_poll; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 88 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 89 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 90 | if (!remote_hung) { |
| 91 | if (poll_fds[1].revents & POLLIN) { |
| 92 | sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 93 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 94 | sz += b; |
| 95 | // Log one line at a time |
| 96 | for (b = 0; b < sz; b++) { |
| 97 | if (buffer[b] == '\r') { |
| 98 | buffer[b] = '\0'; |
| 99 | } else if (buffer[b] == '\n') { |
| 100 | buffer[b] = '\0'; |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 101 | if (logwrap) |
Rom Lemarchand | f5200c0 | 2013-01-24 10:57:44 -0800 | [diff] [blame] | 102 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 103 | a = b + 1; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (a == 0 && b == sizeof(buffer) - 1) { |
| 108 | // buffer is full, flush |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 109 | buffer[b] = '\0'; |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 110 | if (logwrap) |
Rom Lemarchand | f5200c0 | 2013-01-24 10:57:44 -0800 | [diff] [blame] | 111 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 112 | b = 0; |
| 113 | } else if (a != b) { |
| 114 | // Keep left-overs |
| 115 | b -= a; |
| 116 | memmove(buffer, &buffer[a], b); |
| 117 | a = 0; |
| 118 | } else { |
| 119 | a = 0; |
| 120 | b = 0; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 124 | if (poll_fds[1].revents & POLLHUP) { |
| 125 | remote_hung = true; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 129 | if (poll_fds[0].revents & POLLIN) { |
| 130 | char tmp[32]; |
| 131 | int ret; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 132 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 133 | read(signal_fd, tmp, sizeof(tmp)); |
| 134 | while (!found_child) { |
| 135 | do { |
| 136 | ret = waitpid(-1, &status, WNOHANG); |
| 137 | } while (ret < 0 && errno == EINTR); |
| 138 | |
| 139 | if (ret <= 0) |
| 140 | break; |
| 141 | |
| 142 | found_child = (pid == ret); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 143 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 144 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 145 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 146 | |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame^] | 147 | if (chld_sts != NULL) { |
| 148 | *chld_sts = status; |
| 149 | } else { |
| 150 | if (WIFEXITED(status)) |
| 151 | rc = WEXITSTATUS(status); |
| 152 | else |
| 153 | rc = -ECHILD; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 154 | } |
| 155 | |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame^] | 156 | if (logwrap) { |
| 157 | // Flush remaining data |
| 158 | if (a != b) { |
| 159 | buffer[b] = '\0'; |
| 160 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
| 161 | } |
| 162 | if (WIFEXITED(status)) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 163 | if (WEXITSTATUS(status)) |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame^] | 164 | ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag, |
| 165 | WEXITSTATUS(status)); |
| 166 | } else { |
Rom Lemarchand | 2f34c50 | 2013-02-07 15:43:09 -0800 | [diff] [blame] | 167 | if (WIFSIGNALED(status)) |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame^] | 168 | ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag, |
| 169 | WTERMSIG(status)); |
Rom Lemarchand | 2f34c50 | 2013-02-07 15:43:09 -0800 | [diff] [blame] | 170 | else if (WIFSTOPPED(status)) |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame^] | 171 | ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag, |
| 172 | WSTOPSIG(status)); |
| 173 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 174 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 175 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 176 | err_poll: |
| 177 | return rc; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 180 | static void child(int argc, char* argv[], bool logwrap) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 181 | // create null terminated argv_child array |
| 182 | char* argv_child[argc + 1]; |
| 183 | memcpy(argv_child, argv, argc * sizeof(char *)); |
| 184 | argv_child[argc] = NULL; |
| 185 | |
| 186 | if (execvp(argv_child[0], argv_child)) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 187 | FATAL_CHILD("executing %s failed: %s\n", argv_child[0], |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 188 | strerror(errno)); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 192 | static void sigchld_handler(int sig) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 193 | write(signal_fd_write, &sig, 1); |
| 194 | } |
| 195 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 196 | int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit, |
| 197 | bool logwrap) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 198 | pid_t pid; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 199 | int parent_ptty; |
| 200 | int child_ptty; |
| 201 | char *child_devname = NULL; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 202 | struct sigaction chldact; |
| 203 | struct sigaction oldchldact; |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 204 | struct sigaction intact; |
| 205 | struct sigaction quitact; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 206 | sigset_t blockset; |
| 207 | sigset_t oldset; |
| 208 | int sockets[2]; |
| 209 | int rc = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 210 | |
| 211 | /* Use ptty instead of socketpair so that STDOUT is not buffered */ |
| 212 | parent_ptty = open("/dev/ptmx", O_RDWR); |
| 213 | if (parent_ptty < 0) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 214 | ERROR("Cannot create parent ptty\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 215 | rc = -1; |
| 216 | goto err_open; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (grantpt(parent_ptty) || unlockpt(parent_ptty) || |
| 220 | ((child_devname = (char*)ptsname(parent_ptty)) == 0)) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 221 | ERROR("Problem with /dev/ptmx\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 222 | rc = -1; |
| 223 | goto err_ptty; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 226 | sigemptyset(&blockset); |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 227 | sigaddset(&blockset, SIGINT); |
| 228 | sigaddset(&blockset, SIGQUIT); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 229 | sigaddset(&blockset, SIGCHLD); |
Rom Lemarchand | ed179d2 | 2013-01-16 15:07:30 -0800 | [diff] [blame] | 230 | pthread_sigmask(SIG_BLOCK, &blockset, &oldset); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 231 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 232 | pid = fork(); |
| 233 | if (pid < 0) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 234 | ERROR("Failed to fork\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 235 | rc = -1; |
| 236 | goto err_fork; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 237 | } else if (pid == 0) { |
Rom Lemarchand | ed179d2 | 2013-01-16 15:07:30 -0800 | [diff] [blame] | 238 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 239 | close(parent_ptty); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 240 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 241 | child_ptty = open(child_devname, O_RDWR); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 242 | if (child_ptty < 0) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 243 | FATAL_CHILD("Problem with child ptty\n"); |
Rom Lemarchand | f5200c0 | 2013-01-24 10:57:44 -0800 | [diff] [blame] | 244 | return -1; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // redirect stdout and stderr |
| 248 | dup2(child_ptty, 1); |
| 249 | dup2(child_ptty, 2); |
| 250 | close(child_ptty); |
| 251 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 252 | child(argc, argv, logwrap); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 253 | } else { |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 254 | struct sigaction ignact; |
| 255 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 256 | memset(&chldact, 0, sizeof(chldact)); |
| 257 | chldact.sa_handler = sigchld_handler; |
| 258 | chldact.sa_flags = SA_NOCLDSTOP; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 259 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 260 | sigaction(SIGCHLD, &chldact, &oldchldact); |
| 261 | if ((!(oldchldact.sa_flags & SA_SIGINFO) && |
| 262 | oldchldact.sa_handler != SIG_DFL && |
| 263 | oldchldact.sa_handler != SIG_IGN) || |
| 264 | ((oldchldact.sa_flags & SA_SIGINFO) && |
| 265 | oldchldact.sa_sigaction != NULL)) { |
| 266 | ALOG(LOG_WARN, "logwrapper", "logwrap replaced the SIGCHLD " |
| 267 | "handler and might cause interaction issues"); |
| 268 | } |
| 269 | |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 270 | if (ignore_int_quit) { |
| 271 | memset(&ignact, 0, sizeof(ignact)); |
| 272 | ignact.sa_handler = SIG_IGN; |
| 273 | sigaction(SIGINT, &ignact, &intact); |
| 274 | sigaction(SIGQUIT, &ignact, &quitact); |
| 275 | } |
| 276 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 277 | rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets); |
| 278 | if (rc == -1) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 279 | ERROR("socketpair failed: %s\n", strerror(errno)); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 280 | goto err_socketpair; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 283 | fcntl(sockets[0], F_SETFD, FD_CLOEXEC); |
| 284 | fcntl(sockets[0], F_SETFL, O_NONBLOCK); |
| 285 | fcntl(sockets[1], F_SETFD, FD_CLOEXEC); |
| 286 | fcntl(sockets[1], F_SETFL, O_NONBLOCK); |
| 287 | |
| 288 | signal_fd_write = sockets[0]; |
| 289 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 290 | rc = parent(argv[0], parent_ptty, sockets[1], pid, status, logwrap); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 291 | } |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 292 | |
| 293 | close(sockets[0]); |
| 294 | close(sockets[1]); |
| 295 | err_socketpair: |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 296 | if (ignore_int_quit) { |
| 297 | sigaction(SIGINT, &intact, NULL); |
| 298 | sigaction(SIGQUIT, &quitact, NULL); |
| 299 | } |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 300 | sigaction(SIGCHLD, &oldchldact, NULL); |
| 301 | err_fork: |
Rom Lemarchand | ed179d2 | 2013-01-16 15:07:30 -0800 | [diff] [blame] | 302 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 303 | err_ptty: |
| 304 | close(parent_ptty); |
| 305 | err_open: |
| 306 | return rc; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 307 | } |