blob: 5337801e3ae231cb10dae6c4ea94570db34bfdc8 [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
Rom Lemarchand113bd472013-01-10 15:21:18 -080017#include <errno.h>
18#include <fcntl.h>
19#include <libgen.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070020#include <poll.h>
Rom Lemarchand74a7b912013-03-11 14:00:58 -070021#include <pthread.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/socket.h>
26#include <sys/types.h>
27#include <sys/wait.h>
28#include <unistd.h>
Rom Lemarchand113bd472013-01-10 15:21:18 -080029
Tom Cherryff7d0672019-09-25 15:01:58 -070030#include <algorithm>
31
32#include <android-base/macros.h>
Ken Sumrall96e11b52013-04-03 13:45:10 -070033#include <cutils/klog.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080034#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070035#include <logwrap/logwrap.h>
Rom Lemarchand113bd472013-01-10 15:21:18 -080036
Rom Lemarchand74a7b912013-03-11 14:00:58 -070037static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
Tom Cherry01328932019-09-24 18:58:38 -070038// Protected by fd_mutex. These signals must be blocked while modifying as well.
39static pid_t child_pid;
40static struct sigaction old_int;
41static struct sigaction old_quit;
42static struct sigaction old_hup;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080043
Tom Cherryff7d0672019-09-25 15:01:58 -070044#define ERROR(fmt, args...) \
45 do { \
46 fprintf(stderr, fmt, ##args); \
47 ALOG(LOG_ERROR, "logwrapper", fmt, ##args); \
48 } while (0)
Rom Lemarchand99e19662013-01-07 15:50:02 -080049
Tom Cherryff7d0672019-09-25 15:01:58 -070050#define FATAL_CHILD(fmt, args...) \
51 do { \
52 ERROR(fmt, ##args); \
53 _exit(-1); \
54 } while (0)
Rom Lemarchand113bd472013-01-10 15:21:18 -080055
Ken Sumrall96e11b52013-04-03 13:45:10 -070056#define MAX_KLOG_TAG 16
57
58/* This is a simple buffer that holds up to the first beginning_buf->buf_size
59 * bytes of output from a command.
60 */
61#define BEGINNING_BUF_SIZE 0x1000
62struct beginning_buf {
Tom Cherryff7d0672019-09-25 15:01:58 -070063 char* buf;
Ken Sumrall96e11b52013-04-03 13:45:10 -070064 size_t alloc_len;
65 /* buf_size is the usable space, which is one less than the allocated size */
66 size_t buf_size;
67 size_t used_len;
68};
69
70/* This is a circular buf that holds up to the last ending_buf->buf_size bytes
71 * of output from a command after the first beginning_buf->buf_size bytes
72 * (which are held in beginning_buf above).
73 */
74#define ENDING_BUF_SIZE 0x1000
75struct ending_buf {
Tom Cherryff7d0672019-09-25 15:01:58 -070076 char* buf;
Ken Sumrall96e11b52013-04-03 13:45:10 -070077 ssize_t alloc_len;
78 /* buf_size is the usable space, which is one less than the allocated size */
79 ssize_t buf_size;
80 ssize_t used_len;
81 /* read and write offsets into the circular buffer */
82 int read;
83 int write;
84};
85
Tom Cherryff7d0672019-09-25 15:01:58 -070086/* A structure to hold all the abbreviated buf data */
Ken Sumrall96e11b52013-04-03 13:45:10 -070087struct abbr_buf {
88 struct beginning_buf b_buf;
89 struct ending_buf e_buf;
90 int beginning_buf_full;
91};
92
93/* Collect all the various bits of info needed for logging in one place. */
94struct log_info {
95 int log_target;
96 char klog_fmt[MAX_KLOG_TAG * 2];
Tom Cherryff7d0672019-09-25 15:01:58 -070097 const char* btag;
Ken Sumrall96e11b52013-04-03 13:45:10 -070098 bool abbreviated;
Tom Cherryff7d0672019-09-25 15:01:58 -070099 FILE* fp;
Ken Sumrall96e11b52013-04-03 13:45:10 -0700100 struct abbr_buf a_buf;
101};
102
103/* Forware declaration */
Tom Cherryff7d0672019-09-25 15:01:58 -0700104static void add_line_to_abbr_buf(struct abbr_buf* a_buf, char* linebuf, int linelen);
Ken Sumrall96e11b52013-04-03 13:45:10 -0700105
106/* Return 0 on success, and 1 when full */
Tom Cherryff7d0672019-09-25 15:01:58 -0700107static int add_line_to_linear_buf(struct beginning_buf* b_buf, char* line, ssize_t line_len) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700108 int full = 0;
109
110 if ((line_len + b_buf->used_len) > b_buf->buf_size) {
111 full = 1;
112 } else {
113 /* Add to the end of the buf */
114 memcpy(b_buf->buf + b_buf->used_len, line, line_len);
115 b_buf->used_len += line_len;
116 }
117
118 return full;
119}
120
Tom Cherryff7d0672019-09-25 15:01:58 -0700121static void add_line_to_circular_buf(struct ending_buf* e_buf, char* line, ssize_t line_len) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700122 ssize_t free_len;
123 ssize_t needed_space;
Ken Sumrall96e11b52013-04-03 13:45:10 -0700124 int cnt;
125
Tom Cherryff7d0672019-09-25 15:01:58 -0700126 if (e_buf->buf == nullptr) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700127 return;
128 }
129
Tom Cherryff7d0672019-09-25 15:01:58 -0700130 if (line_len > e_buf->buf_size) {
131 return;
132 }
Ken Sumrall96e11b52013-04-03 13:45:10 -0700133
134 free_len = e_buf->buf_size - e_buf->used_len;
135
136 if (line_len > free_len) {
137 /* remove oldest entries at read, and move read to make
138 * room for the new string */
139 needed_space = line_len - free_len;
140 e_buf->read = (e_buf->read + needed_space) % e_buf->buf_size;
141 e_buf->used_len -= needed_space;
142 }
143
144 /* Copy the line into the circular buffer, dealing with possible
145 * wraparound.
146 */
Tom Cherryff7d0672019-09-25 15:01:58 -0700147 cnt = std::min(line_len, e_buf->buf_size - e_buf->write);
Ken Sumrall96e11b52013-04-03 13:45:10 -0700148 memcpy(e_buf->buf + e_buf->write, line, cnt);
149 if (cnt < line_len) {
150 memcpy(e_buf->buf, line + cnt, line_len - cnt);
151 }
152 e_buf->used_len += line_len;
153 e_buf->write = (e_buf->write + line_len) % e_buf->buf_size;
154}
155
156/* Log directly to the specified log */
Tom Cherryff7d0672019-09-25 15:01:58 -0700157static void do_log_line(struct log_info* log_info, const char* line) {
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700158 if (log_info->log_target & LOG_KLOG) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700159 klog_write(6, log_info->klog_fmt, line);
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700160 }
161 if (log_info->log_target & LOG_ALOG) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700162 ALOG(LOG_INFO, log_info->btag, "%s", line);
163 }
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700164 if (log_info->log_target & LOG_FILE) {
165 fprintf(log_info->fp, "%s\n", line);
166 }
Ken Sumrall96e11b52013-04-03 13:45:10 -0700167}
168
169/* Log to either the abbreviated buf, or directly to the specified log
170 * via do_log_line() above.
171 */
Tom Cherryff7d0672019-09-25 15:01:58 -0700172static void log_line(struct log_info* log_info, char* line, int len) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700173 if (log_info->abbreviated) {
174 add_line_to_abbr_buf(&log_info->a_buf, line, len);
175 } else {
176 do_log_line(log_info, line);
177 }
178}
179
180/*
181 * The kernel will take a maximum of 1024 bytes in any single write to
182 * the kernel logging device file, so find and print each line one at
183 * a time. The allocated size for buf should be at least 1 byte larger
184 * than buf_size (the usable size of the buffer) to make sure there is
185 * room to temporarily stuff a null byte to terminate a line for logging.
186 */
Tom Cherryff7d0672019-09-25 15:01:58 -0700187static void print_buf_lines(struct log_info* log_info, char* buf, int buf_size) {
188 char* line_start;
Ken Sumrall96e11b52013-04-03 13:45:10 -0700189 char c;
Ken Sumrall96e11b52013-04-03 13:45:10 -0700190 int i;
191
192 line_start = buf;
193 for (i = 0; i < buf_size; i++) {
194 if (*(buf + i) == '\n') {
195 /* Found a line ending, print the line and compute new line_start */
196 /* Save the next char and replace with \0 */
197 c = *(buf + i + 1);
198 *(buf + i + 1) = '\0';
199 do_log_line(log_info, line_start);
200 /* Restore the saved char */
201 *(buf + i + 1) = c;
202 line_start = buf + i + 1;
203 } else if (*(buf + i) == '\0') {
204 /* The end of the buffer, print the last bit */
205 do_log_line(log_info, line_start);
206 break;
207 }
208 }
209 /* If the buffer was completely full, and didn't end with a newline, just
210 * ignore the partial last line.
211 */
212}
213
Tom Cherryff7d0672019-09-25 15:01:58 -0700214static void init_abbr_buf(struct abbr_buf* a_buf) {
215 char* new_buf;
Ken Sumrall96e11b52013-04-03 13:45:10 -0700216
217 memset(a_buf, 0, sizeof(struct abbr_buf));
Tom Cherryff7d0672019-09-25 15:01:58 -0700218 new_buf = static_cast<char*>(malloc(BEGINNING_BUF_SIZE));
Ken Sumrall96e11b52013-04-03 13:45:10 -0700219 if (new_buf) {
220 a_buf->b_buf.buf = new_buf;
221 a_buf->b_buf.alloc_len = BEGINNING_BUF_SIZE;
222 a_buf->b_buf.buf_size = BEGINNING_BUF_SIZE - 1;
223 }
Tom Cherryff7d0672019-09-25 15:01:58 -0700224 new_buf = static_cast<char*>(malloc(ENDING_BUF_SIZE));
Ken Sumrall96e11b52013-04-03 13:45:10 -0700225 if (new_buf) {
226 a_buf->e_buf.buf = new_buf;
227 a_buf->e_buf.alloc_len = ENDING_BUF_SIZE;
228 a_buf->e_buf.buf_size = ENDING_BUF_SIZE - 1;
229 }
230}
231
Tom Cherryff7d0672019-09-25 15:01:58 -0700232static void free_abbr_buf(struct abbr_buf* a_buf) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700233 free(a_buf->b_buf.buf);
234 free(a_buf->e_buf.buf);
235}
236
Tom Cherryff7d0672019-09-25 15:01:58 -0700237static void add_line_to_abbr_buf(struct abbr_buf* a_buf, char* linebuf, int linelen) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700238 if (!a_buf->beginning_buf_full) {
Tom Cherryff7d0672019-09-25 15:01:58 -0700239 a_buf->beginning_buf_full = add_line_to_linear_buf(&a_buf->b_buf, linebuf, linelen);
Ken Sumrall96e11b52013-04-03 13:45:10 -0700240 }
241 if (a_buf->beginning_buf_full) {
242 add_line_to_circular_buf(&a_buf->e_buf, linebuf, linelen);
243 }
244}
245
Tom Cherryff7d0672019-09-25 15:01:58 -0700246static void print_abbr_buf(struct log_info* log_info) {
247 struct abbr_buf* a_buf = &log_info->a_buf;
Ken Sumrall96e11b52013-04-03 13:45:10 -0700248
249 /* Add the abbreviated output to the kernel log */
250 if (a_buf->b_buf.alloc_len) {
251 print_buf_lines(log_info, a_buf->b_buf.buf, a_buf->b_buf.used_len);
252 }
253
254 /* Print an ellipsis to indicate that the buffer has wrapped or
255 * is full, and some data was not logged.
256 */
257 if (a_buf->e_buf.used_len == a_buf->e_buf.buf_size) {
258 do_log_line(log_info, "...\n");
259 }
260
261 if (a_buf->e_buf.used_len == 0) {
262 return;
263 }
264
265 /* Simplest way to print the circular buffer is allocate a second buf
266 * of the same size, and memcpy it so it's a simple linear buffer,
267 * and then cal print_buf_lines on it */
268 if (a_buf->e_buf.read < a_buf->e_buf.write) {
269 /* no wrap around, just print it */
Tom Cherryff7d0672019-09-25 15:01:58 -0700270 print_buf_lines(log_info, a_buf->e_buf.buf + a_buf->e_buf.read, a_buf->e_buf.used_len);
Ken Sumrall96e11b52013-04-03 13:45:10 -0700271 } else {
272 /* The circular buffer will always have at least 1 byte unused,
273 * so by allocating alloc_len here we will have at least
274 * 1 byte of space available as required by print_buf_lines().
275 */
Tom Cherryff7d0672019-09-25 15:01:58 -0700276 char* nbuf = static_cast<char*>(malloc(a_buf->e_buf.alloc_len));
Ken Sumrall96e11b52013-04-03 13:45:10 -0700277 if (!nbuf) {
278 return;
279 }
280 int first_chunk_len = a_buf->e_buf.buf_size - a_buf->e_buf.read;
281 memcpy(nbuf, a_buf->e_buf.buf + a_buf->e_buf.read, first_chunk_len);
282 /* copy second chunk */
283 memcpy(nbuf + first_chunk_len, a_buf->e_buf.buf, a_buf->e_buf.write);
284 print_buf_lines(log_info, nbuf, first_chunk_len + a_buf->e_buf.write);
285 free(nbuf);
286 }
287}
288
Tom Cherry01328932019-09-24 18:58:38 -0700289static void signal_handler(int signal_num);
290
291static void block_signals(sigset_t* oldset) {
292 sigset_t blockset;
293
294 sigemptyset(&blockset);
295 sigaddset(&blockset, SIGINT);
296 sigaddset(&blockset, SIGQUIT);
297 sigaddset(&blockset, SIGHUP);
298 pthread_sigmask(SIG_BLOCK, &blockset, oldset);
299}
300
301static void unblock_signals(sigset_t* oldset) {
Tom Cherryff7d0672019-09-25 15:01:58 -0700302 pthread_sigmask(SIG_SETMASK, oldset, nullptr);
Tom Cherry01328932019-09-24 18:58:38 -0700303}
304
305static void setup_signal_handlers(pid_t pid) {
306 struct sigaction handler = {.sa_handler = signal_handler};
307
308 child_pid = pid;
309 sigaction(SIGINT, &handler, &old_int);
310 sigaction(SIGQUIT, &handler, &old_quit);
311 sigaction(SIGHUP, &handler, &old_hup);
312}
313
314static void restore_signal_handlers() {
Tom Cherryff7d0672019-09-25 15:01:58 -0700315 sigaction(SIGINT, &old_int, nullptr);
316 sigaction(SIGQUIT, &old_quit, nullptr);
317 sigaction(SIGHUP, &old_hup, nullptr);
Tom Cherry01328932019-09-24 18:58:38 -0700318 child_pid = 0;
319}
320
321static void signal_handler(int signal_num) {
322 if (child_pid == 0 || kill(child_pid, signal_num) != 0) {
323 restore_signal_handlers();
324 raise(signal_num);
325 }
326}
327
328static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, int log_target,
Tom Cherryff7d0672019-09-25 15:01:58 -0700329 bool abbreviated, const char* file_path, bool forward_signals) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800330 int status = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800331 char buffer[4096];
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800332 struct pollfd poll_fds[] = {
Tom Cherryff7d0672019-09-25 15:01:58 -0700333 {
334 .fd = parent_read,
335 .events = POLLIN,
336 },
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800337 };
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800338 int rc = 0;
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700339 int fd;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800340
Ken Sumrall96e11b52013-04-03 13:45:10 -0700341 struct log_info log_info;
342
Rom Lemarchand113bd472013-01-10 15:21:18 -0800343 int a = 0; // start index of unprocessed data
344 int b = 0; // end index of unprocessed data
345 int sz;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800346 bool found_child = false;
Tom Cherry01328932019-09-24 18:58:38 -0700347 // There is a very small chance that opening child_ptty in the child will fail, but in this case
348 // POLLHUP will not be generated below. Therefore, we use a 1 second timeout for poll() until
349 // we receive a message from child_ptty. If this times out, we call waitpid() with WNOHANG to
350 // check the status of the child process and exit appropriately if it has terminated.
351 bool received_messages = false;
Ken Sumrall96e11b52013-04-03 13:45:10 -0700352 char tmpbuf[256];
Rom Lemarchand113bd472013-01-10 15:21:18 -0800353
Ken Sumrall96e11b52013-04-03 13:45:10 -0700354 log_info.btag = basename(tag);
355 if (!log_info.btag) {
Tom Cherryff7d0672019-09-25 15:01:58 -0700356 log_info.btag = tag;
Ken Sumrall96e11b52013-04-03 13:45:10 -0700357 }
358
359 if (abbreviated && (log_target == LOG_NONE)) {
360 abbreviated = 0;
361 }
362 if (abbreviated) {
363 init_abbr_buf(&log_info.a_buf);
364 }
365
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700366 if (log_target & LOG_KLOG) {
Tom Cherryff7d0672019-09-25 15:01:58 -0700367 snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt), "<6>%.*s: %%s\n", MAX_KLOG_TAG,
368 log_info.btag);
Ken Sumrall96e11b52013-04-03 13:45:10 -0700369 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800370
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700371 if ((log_target & LOG_FILE) && !file_path) {
372 /* No file_path specified, clear the LOG_FILE bit */
373 log_target &= ~LOG_FILE;
374 }
375
376 if (log_target & LOG_FILE) {
377 fd = open(file_path, O_WRONLY | O_CREAT, 0664);
378 if (fd < 0) {
379 ERROR("Cannot log to file %s\n", file_path);
380 log_target &= ~LOG_FILE;
381 } else {
382 lseek(fd, 0, SEEK_END);
383 log_info.fp = fdopen(fd, "a");
384 }
385 }
386
387 log_info.log_target = log_target;
388 log_info.abbreviated = abbreviated;
389
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800390 while (!found_child) {
Tom Cherry01328932019-09-24 18:58:38 -0700391 int timeout = received_messages ? -1 : 1000;
Tom Cherryff7d0672019-09-25 15:01:58 -0700392 if (TEMP_FAILURE_RETRY(poll(poll_fds, arraysize(poll_fds), timeout)) < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800393 ERROR("poll failed\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800394 rc = -1;
395 goto err_poll;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800396 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800397
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700398 if (poll_fds[0].revents & POLLIN) {
Tom Cherry01328932019-09-24 18:58:38 -0700399 received_messages = true;
Tom Cherryff7d0672019-09-25 15:01:58 -0700400 sz = TEMP_FAILURE_RETRY(read(parent_read, &buffer[b], sizeof(buffer) - 1 - b));
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800401
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700402 sz += b;
403 // Log one line at a time
404 for (b = 0; b < sz; b++) {
405 if (buffer[b] == '\r') {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700406 if (abbreviated) {
407 /* The abbreviated logging code uses newline as
408 * the line separator. Lucikly, the pty layer
409 * helpfully cooks the output of the command
410 * being run and inserts a CR before NL. So
411 * I just change it to NL here when doing
412 * abbreviated logging.
413 */
414 buffer[b] = '\n';
415 } else {
416 buffer[b] = '\0';
417 }
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700418 } else if (buffer[b] == '\n') {
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800419 buffer[b] = '\0';
Ken Sumrall96e11b52013-04-03 13:45:10 -0700420 log_line(&log_info, &buffer[a], b - a);
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700421 a = b + 1;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800422 }
423 }
424
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700425 if (a == 0 && b == sizeof(buffer) - 1) {
426 // buffer is full, flush
427 buffer[b] = '\0';
Ken Sumrall96e11b52013-04-03 13:45:10 -0700428 log_line(&log_info, &buffer[a], b - a);
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700429 b = 0;
430 } else if (a != b) {
431 // Keep left-overs
432 b -= a;
433 memmove(buffer, &buffer[a], b);
434 a = 0;
435 } else {
436 a = 0;
437 b = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800438 }
439 }
440
Tom Cherry01328932019-09-24 18:58:38 -0700441 if (!received_messages || (poll_fds[0].revents & POLLHUP)) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800442 int ret;
Tom Cherry01328932019-09-24 18:58:38 -0700443 sigset_t oldset;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800444
Tom Cherry01328932019-09-24 18:58:38 -0700445 if (forward_signals) {
446 // Our signal handlers forward these signals to 'child_pid', but waitpid() may reap
447 // the child, so we must block these signals until we either 1) conclude that the
448 // child is still running or 2) determine the child has been reaped and we have
449 // reset the signals to their original disposition.
450 block_signals(&oldset);
451 }
452
453 int flags = (poll_fds[0].revents & POLLHUP) ? 0 : WNOHANG;
454 ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, flags));
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700455 if (ret < 0) {
456 rc = errno;
457 ALOG(LOG_ERROR, "logwrap", "waitpid failed with %s\n", strerror(errno));
458 goto err_waitpid;
459 }
460 if (ret > 0) {
461 found_child = true;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800462 }
Tom Cherry01328932019-09-24 18:58:38 -0700463
464 if (forward_signals) {
465 if (found_child) {
466 restore_signal_handlers();
467 }
468 unblock_signals(&oldset);
469 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800470 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800471 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800472
Tom Cherryff7d0672019-09-25 15:01:58 -0700473 if (chld_sts != nullptr) {
JP Abgralla689d132013-02-13 16:31:58 -0800474 *chld_sts = status;
475 } else {
Tom Cherryff7d0672019-09-25 15:01:58 -0700476 if (WIFEXITED(status))
477 rc = WEXITSTATUS(status);
478 else
479 rc = -ECHILD;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800480 }
481
Ken Sumrall96e11b52013-04-03 13:45:10 -0700482 // Flush remaining data
483 if (a != b) {
Tom Cherryff7d0672019-09-25 15:01:58 -0700484 buffer[b] = '\0';
485 log_line(&log_info, &buffer[a], b - a);
Ken Sumrall96e11b52013-04-03 13:45:10 -0700486 }
487
488 /* All the output has been processed, time to dump the abbreviated output */
489 if (abbreviated) {
490 print_abbr_buf(&log_info);
491 }
492
493 if (WIFEXITED(status)) {
Tom Cherryff7d0672019-09-25 15:01:58 -0700494 if (WEXITSTATUS(status)) {
495 snprintf(tmpbuf, sizeof(tmpbuf), "%s terminated by exit(%d)\n", log_info.btag,
496 WEXITSTATUS(status));
497 do_log_line(&log_info, tmpbuf);
498 }
Ken Sumrall96e11b52013-04-03 13:45:10 -0700499 } else {
Tom Cherryff7d0672019-09-25 15:01:58 -0700500 if (WIFSIGNALED(status)) {
501 snprintf(tmpbuf, sizeof(tmpbuf), "%s terminated by signal %d\n", log_info.btag,
502 WTERMSIG(status));
503 do_log_line(&log_info, tmpbuf);
504 } else if (WIFSTOPPED(status)) {
505 snprintf(tmpbuf, sizeof(tmpbuf), "%s stopped by signal %d\n", log_info.btag,
506 WSTOPSIG(status));
507 do_log_line(&log_info, tmpbuf);
508 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800509 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800510
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700511err_waitpid:
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800512err_poll:
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700513 if (log_target & LOG_FILE) {
514 fclose(log_info.fp); /* Also closes underlying fd */
515 }
Ken Sumrall96e11b52013-04-03 13:45:10 -0700516 if (abbreviated) {
517 free_abbr_buf(&log_info.a_buf);
518 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800519 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800520}
521
Tom Cherryff7d0672019-09-25 15:01:58 -0700522static void child(int argc, const char* const* argv) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800523 // create null terminated argv_child array
524 char* argv_child[argc + 1];
Tom Cherryff7d0672019-09-25 15:01:58 -0700525 memcpy(argv_child, argv, argc * sizeof(char*));
526 argv_child[argc] = nullptr;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800527
528 if (execvp(argv_child[0], argv_child)) {
Tom Cherryff7d0672019-09-25 15:01:58 -0700529 FATAL_CHILD("executing %s failed: %s\n", argv_child[0], strerror(errno));
Rom Lemarchand113bd472013-01-10 15:21:18 -0800530 }
531}
532
Tom Cherryff7d0672019-09-25 15:01:58 -0700533int logwrap_fork_execvp(int argc, const char* const* argv, int* status, bool forward_signals,
534 int log_target, bool abbreviated, const char* file_path) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800535 pid_t pid;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800536 int parent_ptty;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800537 sigset_t oldset;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800538 int rc = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800539
Rom Lemarchand74a7b912013-03-11 14:00:58 -0700540 rc = pthread_mutex_lock(&fd_mutex);
541 if (rc) {
542 ERROR("failed to lock signal_fd mutex\n");
543 goto err_lock;
544 }
545
Rom Lemarchand113bd472013-01-10 15:21:18 -0800546 /* Use ptty instead of socketpair so that STDOUT is not buffered */
Tom Cherry01328932019-09-24 18:58:38 -0700547 parent_ptty = TEMP_FAILURE_RETRY(posix_openpt(O_RDWR | O_CLOEXEC));
Rom Lemarchand113bd472013-01-10 15:21:18 -0800548 if (parent_ptty < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800549 ERROR("Cannot create parent ptty\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800550 rc = -1;
551 goto err_open;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800552 }
553
Elliott Hughes84cfd102014-07-29 11:05:18 -0700554 char child_devname[64];
Rom Lemarchand113bd472013-01-10 15:21:18 -0800555 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
Tom Cherryff7d0672019-09-25 15:01:58 -0700556 ptsname_r(parent_ptty, child_devname, sizeof(child_devname)) != 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800557 ERROR("Problem with /dev/ptmx\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800558 rc = -1;
559 goto err_ptty;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800560 }
561
Tom Cherry01328932019-09-24 18:58:38 -0700562 if (forward_signals) {
563 // Block these signals until we have the child pid and our signal handlers set up.
564 block_signals(&oldset);
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700565 }
566
Rom Lemarchand113bd472013-01-10 15:21:18 -0800567 pid = fork();
568 if (pid < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800569 ERROR("Failed to fork\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800570 rc = -1;
571 goto err_fork;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800572 } else if (pid == 0) {
Rom Lemarchand74a7b912013-03-11 14:00:58 -0700573 pthread_mutex_unlock(&fd_mutex);
Tom Cherryff7d0672019-09-25 15:01:58 -0700574 if (forward_signals) {
575 unblock_signals(&oldset);
576 }
Tom Cherry01328932019-09-24 18:58:38 -0700577
578 setsid();
579
580 int child_ptty = TEMP_FAILURE_RETRY(open(child_devname, O_RDWR | O_CLOEXEC));
581 if (child_ptty < 0) {
582 FATAL_CHILD("Cannot open child_ptty: %s\n", strerror(errno));
583 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800584 close(parent_ptty);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800585
Rom Lemarchand113bd472013-01-10 15:21:18 -0800586 dup2(child_ptty, 1);
587 dup2(child_ptty, 2);
588 close(child_ptty);
589
Ken Sumrall96e11b52013-04-03 13:45:10 -0700590 child(argc, argv);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800591 } else {
Tom Cherry01328932019-09-24 18:58:38 -0700592 if (forward_signals) {
593 setup_signal_handlers(pid);
594 unblock_signals(&oldset);
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800595 }
596
Tom Cherry01328932019-09-24 18:58:38 -0700597 rc = parent(argv[0], parent_ptty, pid, status, log_target, abbreviated, file_path,
598 forward_signals);
599
600 if (forward_signals) {
601 restore_signal_handlers();
602 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800603 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800604
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800605err_fork:
Tom Cherry01328932019-09-24 18:58:38 -0700606 if (forward_signals) {
607 unblock_signals(&oldset);
608 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800609err_ptty:
610 close(parent_ptty);
611err_open:
Rom Lemarchand74a7b912013-03-11 14:00:58 -0700612 pthread_mutex_unlock(&fd_mutex);
613err_lock:
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800614 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800615}