blob: c2b36be29de4ad4a0c3f4e47741cd7ac6c1f321e [file] [log] [blame]
Rom Lemarchand113bd472013-01-10 15:21:18 -08001/*
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 Lemarchandb58a8222013-01-09 21:31:25 -080019#include <sys/signalfd.h>
20#include <signal.h>
21#include <poll.h>
Rom Lemarchand113bd472013-01-10 15:21:18 -080022#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>
29
30#include <logwrap/logwrap.h>
31#include "private/android_filesystem_config.h"
32#include "cutils/log.h"
33
Rom Lemarchandb58a8222013-01-09 21:31:25 -080034#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
35
Rom Lemarchand113bd472013-01-10 15:21:18 -080036static int fatal(const char *msg) {
37 fprintf(stderr, "%s", msg);
38 ALOG(LOG_ERROR, "logwrapper", "%s", msg);
39 return -1;
40}
41
Rom Lemarchandb58a8222013-01-09 21:31:25 -080042static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
43 int *chld_sts) {
Rom Lemarchand113bd472013-01-10 15:21:18 -080044 int status;
45 char buffer[4096];
Rom Lemarchandb58a8222013-01-09 21:31:25 -080046 struct pollfd poll_fds[] = {
47 [0] = {
48 .fd = parent_read,
49 .events = POLLIN,
50 },
51 [1] = {
52 .fd = signal_fd,
53 .events = POLLIN,
54 },
55 };
Rom Lemarchand113bd472013-01-10 15:21:18 -080056
57 int a = 0; // start index of unprocessed data
58 int b = 0; // end index of unprocessed data
59 int sz;
60
61 char *btag = basename(tag);
62 if (!btag) btag = (char*) tag;
63
Rom Lemarchandb58a8222013-01-09 21:31:25 -080064 while (1) {
65 if (poll(poll_fds, ARRAY_SIZE(poll_fds), -1) <= 0) {
66 return fatal("poll failed\n");
67 }
Rom Lemarchand113bd472013-01-10 15:21:18 -080068
Rom Lemarchandb58a8222013-01-09 21:31:25 -080069 if (poll_fds[0].revents & POLLIN) {
70 sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b);
71
72 sz += b;
73 // Log one line at a time
74 for (b = 0; b < sz; b++) {
75 if (buffer[b] == '\r') {
76 buffer[b] = '\0';
77 } else if (buffer[b] == '\n') {
78 buffer[b] = '\0';
79 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
80 a = b + 1;
81 }
82 }
83
84 if (a == 0 && b == sizeof(buffer) - 1) {
85 // buffer is full, flush
Rom Lemarchand113bd472013-01-10 15:21:18 -080086 buffer[b] = '\0';
87 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
Rom Lemarchandb58a8222013-01-09 21:31:25 -080088 b = 0;
89 } else if (a != b) {
90 // Keep left-overs
91 b -= a;
92 memmove(buffer, &buffer[a], b);
93 a = 0;
94 } else {
95 a = 0;
96 b = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -080097 }
98 }
99
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800100 if (poll_fds[1].revents & POLLIN) {
101 struct signalfd_siginfo sfd_info;
102 pid_t wpid;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800103
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800104 // Clear all pending signals before reading the child's status
105 while (read(signal_fd, &sfd_info, sizeof(sfd_info)) > 0) {
106 if ((pid_t)sfd_info.ssi_pid != pid)
107 ALOG(LOG_WARN, "logwrapper", "cleared SIGCHLD for pid %u\n",
108 sfd_info.ssi_pid);
109 }
110 wpid = waitpid(pid, &status, WNOHANG);
111 if (wpid > 0)
112 break;
113 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800114 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800115
Rom Lemarchand113bd472013-01-10 15:21:18 -0800116 // Flush remaining data
117 if (a != b) {
118 buffer[b] = '\0';
119 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
120 }
121
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800122 if (WIFEXITED(status)) {
123 if (WEXITSTATUS(status))
Rom Lemarchand113bd472013-01-10 15:21:18 -0800124 ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
125 WEXITSTATUS(status));
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800126 } else if (WIFSIGNALED(status)) {
127 ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag,
128 WTERMSIG(status));
129 } else if (WIFSTOPPED(status)) {
130 ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag,
131 WSTOPSIG(status));
132 }
133 if (chld_sts != NULL)
134 *chld_sts = status;
135
136 return 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800137}
138
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800139static void child(int argc, char* argv[]) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800140 // create null terminated argv_child array
141 char* argv_child[argc + 1];
142 memcpy(argv_child, argv, argc * sizeof(char *));
143 argv_child[argc] = NULL;
144
145 if (execvp(argv_child[0], argv_child)) {
146 ALOG(LOG_ERROR, "logwrapper",
147 "executing %s failed: %s\n", argv_child[0], strerror(errno));
148 exit(-1);
149 }
150}
151
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800152int logwrap(int argc, char* argv[], int *status) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800153 pid_t pid;
154
155 int parent_ptty;
156 int child_ptty;
157 char *child_devname = NULL;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800158 sigset_t chldset;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800159
160 /* Use ptty instead of socketpair so that STDOUT is not buffered */
161 parent_ptty = open("/dev/ptmx", O_RDWR);
162 if (parent_ptty < 0) {
163 return fatal("Cannot create parent ptty\n");
164 }
165
166 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
167 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
168 return fatal("Problem with /dev/ptmx\n");
169 }
170
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800171 sigemptyset(&chldset);
172 sigaddset(&chldset, SIGCHLD);
173 sigprocmask(SIG_BLOCK, &chldset, NULL);
174
Rom Lemarchand113bd472013-01-10 15:21:18 -0800175 pid = fork();
176 if (pid < 0) {
177 close(parent_ptty);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800178 sigprocmask(SIG_UNBLOCK, &chldset, NULL);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800179 return fatal("Failed to fork\n");
180 } else if (pid == 0) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800181 close(parent_ptty);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800182 sigprocmask(SIG_UNBLOCK, &chldset, NULL);
183 child_ptty = open(child_devname, O_RDWR);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800184 if (child_ptty < 0) {
185 return fatal("Problem with child ptty\n");
186 }
187
188 // redirect stdout and stderr
189 dup2(child_ptty, 1);
190 dup2(child_ptty, 2);
191 close(child_ptty);
192
193 child(argc - 1, &argv[1]);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800194 return fatal("This should never happen\n");
Rom Lemarchand113bd472013-01-10 15:21:18 -0800195
196 } else {
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800197 int rc;
198 int fd;
199
200 fd = signalfd(-1, &chldset, SFD_NONBLOCK);
201 if (fd == -1) {
202 char msg[40];
203
204 snprintf(msg, sizeof(msg), "signalfd failed: %d\n", errno);
205
206 close(parent_ptty);
207 sigprocmask(SIG_UNBLOCK, &chldset, NULL);
208 return fatal(msg);
209 }
210
Rom Lemarchand113bd472013-01-10 15:21:18 -0800211 // switch user and group to "log"
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800212 // this may fail if we are not root,
213 // but in that case switching user/group is unnecessary
Rom Lemarchand113bd472013-01-10 15:21:18 -0800214 setgid(AID_LOG);
215 setuid(AID_LOG);
216
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800217 rc = parent(argv[1], parent_ptty, fd, pid, status);
218 close(parent_ptty);
219 close(fd);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800220
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800221 sigprocmask(SIG_UNBLOCK, &chldset, NULL);
222
223 return rc;
224 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800225}