blob: 4307a305532eefa3f69793065219ca58f752e839 [file] [log] [blame]
Rom Lemarchand113bd472013-01-10 15:21:18 -08001/* system/core/include/logwrap/logwrap.h
2 *
3 * Copyright 2013, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef __LIBS_LOGWRAP_H
19#define __LIBS_LOGWRAP_H
20
Rom Lemarchand75c289a2013-01-09 10:20:25 -080021#include <stdbool.h>
22
Rom Lemarchand113bd472013-01-10 15:21:18 -080023__BEGIN_DECLS
24
Rom Lemarchandb58a8222013-01-09 21:31:25 -080025/*
26 * Run a command while logging its stdout and stderr
27 *
28 * WARNING: while this function is running it will clear all SIGCHLD handlers
29 * if you rely on SIGCHLD in the caller there is a chance zombies will be
30 * created if you're not calling waitpid after calling this. This function will
31 * log a warning when it clears SIGCHLD for processes other than the child it
32 * created.
33 *
34 * Arguments:
35 * argc: the number of elements in argv
36 * argv: an array of strings containing the command to be executed and its
37 * arguments as separate strings. argv does not need to be
38 * NULL-terminated
39 * status: the equivalent child status as populated by wait(status). This
Rom Lemarchand2f34c502013-02-07 15:43:09 -080040 * value is only valid when logwrap successfully completes. If NULL
41 * the return value of the child will be the function's return value.
Rom Lemarchand75c289a2013-01-09 10:20:25 -080042 * ignore_int_quit: set to true if you want to completely ignore SIGINT and
43 * SIGQUIT while logwrap is running. This may force the end-user to
44 * send a signal twice to signal the caller (once for the child, and
45 * once for the caller)
Ken Sumrall96e11b52013-04-03 13:45:10 -070046 * log_target: Specify where to log the output of the child, either LOG_NONE,
Ken Sumrall4eaf9052013-09-18 17:49:21 -070047 * LOG_ALOG (for the Android system log), LOG_KLOG (for the kernel
48 * log), or LOG_FILE (and you need to specify a pathname in the
49 * file_path argument, otherwise pass NULL). These are bit fields,
50 * and can be OR'ed together to log to multiple places.
Ken Sumrall96e11b52013-04-03 13:45:10 -070051 * abbreviated: If true, capture up to the first 100 lines and last 4K of
52 * output from the child. The abbreviated output is not dumped to
53 * the specified log until the child has exited.
Ken Sumrall4eaf9052013-09-18 17:49:21 -070054 * file_path: if log_target has the LOG_FILE bit set, then this parameter
55 * must be set to the pathname of the file to log to.
Rom Lemarchandb58a8222013-01-09 21:31:25 -080056 *
57 * Return value:
58 * 0 when logwrap successfully run the child process and captured its status
59 * -1 when an internal error occurred
Rom Lemarchand2f34c502013-02-07 15:43:09 -080060 * -ECHILD if status is NULL and the child didn't exit properly
61 * the return value of the child if it exited properly and status is NULL
Rom Lemarchandb58a8222013-01-09 21:31:25 -080062 *
63 */
Ken Sumrall96e11b52013-04-03 13:45:10 -070064
Ken Sumrall4eaf9052013-09-18 17:49:21 -070065/* Values for the log_target parameter android_fork_execvp_ext() */
Ken Sumrall96e11b52013-04-03 13:45:10 -070066#define LOG_NONE 0
67#define LOG_ALOG 1
68#define LOG_KLOG 2
Ken Sumrall4eaf9052013-09-18 17:49:21 -070069#define LOG_FILE 4
Ken Sumrall96e11b52013-04-03 13:45:10 -070070
71int android_fork_execvp_ext(int argc, char* argv[], int *status, bool ignore_int_quit,
Ken Sumrall4eaf9052013-09-18 17:49:21 -070072 int log_target, bool abbreviated, char *file_path);
Ken Sumrall96e11b52013-04-03 13:45:10 -070073
74/* Similar to above, except abbreviated logging is not available, and if logwrap
75 * is true, logging is to the Android system log, and if false, there is no
76 * logging.
77 */
78static inline int android_fork_execvp(int argc, char* argv[], int *status,
79 bool ignore_int_quit, bool logwrap)
80{
81 return android_fork_execvp_ext(argc, argv, status, ignore_int_quit,
Ken Sumrall4eaf9052013-09-18 17:49:21 -070082 (logwrap ? LOG_ALOG : LOG_NONE), false, NULL);
Ken Sumrall96e11b52013-04-03 13:45:10 -070083}
84
Rom Lemarchand113bd472013-01-10 15:21:18 -080085__END_DECLS
86
87#endif /* __LIBS_LOGWRAP_H */