San Mehat | d183042 | 2010-01-15 08:02:39 -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> |
| 19 | #include <sys/wait.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
| 25 | |
| 26 | #include "private/android_filesystem_config.h" |
| 27 | #include "cutils/log.h" |
| 28 | |
| 29 | int parent(const char *tag, int parent_read) { |
| 30 | int status; |
| 31 | char buffer[4096]; |
| 32 | |
| 33 | int a = 0; // start index of unprocessed data |
| 34 | int b = 0; // end index of unprocessed data |
| 35 | int sz; |
| 36 | while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) { |
| 37 | |
| 38 | sz += b; |
| 39 | // Log one line at a time |
| 40 | for (b = 0; b < sz; b++) { |
| 41 | if (buffer[b] == '\r') { |
| 42 | buffer[b] = '\0'; |
| 43 | } else if (buffer[b] == '\n') { |
| 44 | buffer[b] = '\0'; |
| 45 | |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 46 | ALOG(LOG_INFO, tag, "%s", &buffer[a]); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 47 | a = b + 1; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (a == 0 && b == sizeof(buffer) - 1) { |
| 52 | // buffer is full, flush |
| 53 | buffer[b] = '\0'; |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 54 | ALOG(LOG_INFO, tag, "%s", &buffer[a]); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 55 | b = 0; |
| 56 | } else if (a != b) { |
| 57 | // Keep left-overs |
| 58 | b -= a; |
| 59 | memmove(buffer, &buffer[a], b); |
| 60 | a = 0; |
| 61 | } else { |
| 62 | a = 0; |
| 63 | b = 0; |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | // Flush remaining data |
| 68 | if (a != b) { |
| 69 | buffer[b] = '\0'; |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 70 | ALOG(LOG_INFO, tag, "%s", &buffer[a]); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 71 | } |
| 72 | status = 0xAAAA; |
| 73 | if (wait(&status) != -1) { // Wait for child |
| 74 | if (WIFEXITED(status)) { |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 75 | if (WEXITSTATUS(status) != 0) { |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 76 | ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag, |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 77 | WEXITSTATUS(status)); |
| 78 | } |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 79 | return WEXITSTATUS(status); |
| 80 | } else if (WIFSIGNALED(status)) |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 81 | ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag, |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 82 | WTERMSIG(status)); |
| 83 | else if (WIFSTOPPED(status)) |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 84 | ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag, |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 85 | WSTOPSIG(status)); |
| 86 | } else |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 87 | ALOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag, |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 88 | strerror(errno), errno); |
| 89 | return -EAGAIN; |
| 90 | } |
| 91 | |
| 92 | void child(int argc, const char**argv) { |
| 93 | // create null terminated argv_child array |
| 94 | char* argv_child[argc + 1]; |
| 95 | memcpy(argv_child, argv, argc * sizeof(char *)); |
| 96 | argv_child[argc] = NULL; |
| 97 | |
| 98 | // XXX: PROTECT FROM VIKING KILLER |
| 99 | if (execv(argv_child[0], argv_child)) { |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 100 | ALOG(LOG_ERROR, "logwrapper", |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 101 | "executing %s failed: %s", argv_child[0], strerror(errno)); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 102 | } |
Brad Fitzpatrick | e773228 | 2010-09-20 11:29:47 -0700 | [diff] [blame] | 103 | _exit(1); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | int logwrap(int argc, const char* argv[], int background) |
| 107 | { |
| 108 | pid_t pid; |
| 109 | |
| 110 | int parent_ptty; |
| 111 | int child_ptty; |
Brad Fitzpatrick | e773228 | 2010-09-20 11:29:47 -0700 | [diff] [blame] | 112 | char child_devname[64]; // same size as libc/unistd/ptsname_r.c |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 113 | |
| 114 | /* Use ptty instead of socketpair so that STDOUT is not buffered */ |
| 115 | parent_ptty = open("/dev/ptmx", O_RDWR); |
| 116 | if (parent_ptty < 0) { |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 117 | ALOG(LOG_ERROR, "logwrapper", "Cannot create parent ptty"); |
| 118 | return -errno; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if (grantpt(parent_ptty) || unlockpt(parent_ptty) || |
Brad Fitzpatrick | e773228 | 2010-09-20 11:29:47 -0700 | [diff] [blame] | 122 | ptsname_r(parent_ptty, child_devname, sizeof(child_devname))) { |
Robert Greenwalt | 37dc4a5 | 2010-04-28 16:05:04 -0700 | [diff] [blame] | 123 | close(parent_ptty); |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 124 | ALOG(LOG_ERROR, "logwrapper", "Problem with /dev/ptmx"); |
| 125 | return -1; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | pid = fork(); |
| 129 | if (pid < 0) { |
Robert Greenwalt | 37dc4a5 | 2010-04-28 16:05:04 -0700 | [diff] [blame] | 130 | close(parent_ptty); |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 131 | ALOG(LOG_ERROR, "logwrapper", "Failed to fork"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 132 | return -errno; |
| 133 | } else if (pid == 0) { |
Brad Fitzpatrick | faabd3d | 2010-09-20 11:13:07 -0700 | [diff] [blame] | 134 | /* |
| 135 | * Child |
| 136 | */ |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 137 | child_ptty = open(child_devname, O_RDWR); |
| 138 | if (child_ptty < 0) { |
Robert Greenwalt | 37dc4a5 | 2010-04-28 16:05:04 -0700 | [diff] [blame] | 139 | close(parent_ptty); |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 140 | ALOG(LOG_ERROR, "logwrapper", "Problem with child ptty"); |
Brad Fitzpatrick | e773228 | 2010-09-20 11:29:47 -0700 | [diff] [blame] | 141 | _exit(errno < 128 ? errno : 1); // XXX lame |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 142 | } |
Brad Fitzpatrick | faabd3d | 2010-09-20 11:13:07 -0700 | [diff] [blame] | 143 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 144 | // redirect stdout and stderr |
| 145 | close(parent_ptty); |
| 146 | dup2(child_ptty, 1); |
| 147 | dup2(child_ptty, 2); |
| 148 | close(child_ptty); |
| 149 | |
| 150 | if (background) { |
| 151 | int fd = open("/dev/cpuctl/bg_non_interactive/tasks", O_WRONLY); |
Brad Fitzpatrick | faabd3d | 2010-09-20 11:13:07 -0700 | [diff] [blame] | 152 | if (fd >= 0) { |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 153 | char text[64]; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 154 | sprintf(text, "%d", getpid()); |
| 155 | if (write(fd, text, strlen(text)) < 0) { |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 156 | ALOG(LOG_WARN, "logwrapper", |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 157 | "Unable to background process (%s)", strerror(errno)); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 158 | } |
| 159 | close(fd); |
| 160 | } else { |
Steve Block | bf91ebf | 2011-10-12 17:28:37 +0100 | [diff] [blame] | 161 | ALOG(LOG_WARN, "logwrapper", |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 162 | "Unable to background process (%s)", strerror(errno)); |
| 163 | } |
| 164 | } |
Brad Fitzpatrick | faabd3d | 2010-09-20 11:13:07 -0700 | [diff] [blame] | 165 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 166 | child(argc, argv); |
| 167 | } else { |
Brad Fitzpatrick | faabd3d | 2010-09-20 11:13:07 -0700 | [diff] [blame] | 168 | /* |
| 169 | * Parent |
| 170 | */ |
| 171 | int rc = parent(argv[0], parent_ptty); |
Robert Greenwalt | 37dc4a5 | 2010-04-28 16:05:04 -0700 | [diff] [blame] | 172 | close(parent_ptty); |
Brad Fitzpatrick | faabd3d | 2010-09-20 11:13:07 -0700 | [diff] [blame] | 173 | return rc; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
JP Abgrall | 9e5e0ce | 2011-12-14 15:20:59 -0800 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * The following is based off of bionic/libc/unistd/system.c with |
| 181 | * modifications to avoid calling /system/bin/sh -c |
| 182 | */ |
| 183 | extern char **environ; |
| 184 | int system_nosh(const char *command) |
| 185 | { |
| 186 | pid_t pid; |
| 187 | sig_t intsave, quitsave; |
| 188 | sigset_t mask, omask; |
| 189 | int pstat; |
| 190 | char buffer[255]; |
| 191 | char *argp[32]; |
| 192 | char *next = buffer; |
| 193 | char *tmp; |
| 194 | int i = 0; |
| 195 | |
| 196 | if (!command) /* just checking... */ |
| 197 | return(1); |
| 198 | |
| 199 | /* |
| 200 | * The command to argp splitting is from code that was |
| 201 | * reverted in Change: 11b4e9b2 |
| 202 | */ |
| 203 | if (strnlen(buffer, sizeof(buffer) - 1) == sizeof(buffer) - 1) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 204 | ALOGE("command line too long while processing: %s", command); |
JP Abgrall | 9e5e0ce | 2011-12-14 15:20:59 -0800 | [diff] [blame] | 205 | errno = E2BIG; |
| 206 | return -1; |
| 207 | } |
| 208 | strcpy(buffer, command); // Command len is already checked. |
| 209 | while ((tmp = strsep(&next, " "))) { |
| 210 | argp[i++] = tmp; |
| 211 | if (i == 32) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 212 | ALOGE("argument overflow while processing: %s", command); |
JP Abgrall | 9e5e0ce | 2011-12-14 15:20:59 -0800 | [diff] [blame] | 213 | errno = E2BIG; |
| 214 | return -1; |
| 215 | } |
| 216 | } |
| 217 | argp[i] = NULL; |
| 218 | |
| 219 | |
| 220 | sigemptyset(&mask); |
| 221 | sigaddset(&mask, SIGCHLD); |
| 222 | sigprocmask(SIG_BLOCK, &mask, &omask); |
| 223 | switch (pid = vfork()) { |
| 224 | case -1: /* error */ |
| 225 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 226 | return(-1); |
| 227 | case 0: /* child */ |
| 228 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 229 | execve(argp[0], argp, environ); |
| 230 | _exit(127); |
| 231 | } |
| 232 | |
| 233 | intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN); |
| 234 | quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN); |
| 235 | pid = waitpid(pid, (int *)&pstat, 0); |
| 236 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 237 | (void)bsd_signal(SIGINT, intsave); |
| 238 | (void)bsd_signal(SIGQUIT, quitsave); |
| 239 | return (pid == -1 ? -1 : pstat); |
| 240 | } |