1/* 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
21#include <stdbool.h>
22#include <stdint.h>
23
24__BEGIN_DECLS
25
26/*
27 * Run a command while logging its stdout and stderr
28 *
29 * WARNING: while this function is running it will clear all SIGCHLD handlers
30 * if you rely on SIGCHLD in the caller there is a chance zombies will be
31 * created if you're not calling waitpid after calling this. This function will
32 * log a warning when it clears SIGCHLD for processes other than the child it
33 * created.
34 *
35 * Arguments:
36 *   argc:   the number of elements in argv
37 *   argv:   an array of strings containing the command to be executed and its
38 *           arguments as separate strings. argv does not need to be
39 *           NULL-terminated
40 *   status: the equivalent child status as populated by wait(status). This
41 *           value is only valid when logwrap successfully completes. If NULL
42 *           the return value of the child will be the function's return value.
43 *   ignore_int_quit: set to true if you want to completely ignore SIGINT and
44 *           SIGQUIT while logwrap is running. This may force the end-user to
45 *           send a signal twice to signal the caller (once for the child, and
46 *           once for the caller)
47 *   log_target: Specify where to log the output of the child, either LOG_NONE,
48 *           LOG_ALOG (for the Android system log), LOG_KLOG (for the kernel
49 *           log), or LOG_FILE (and you need to specify a pathname in the
50 *           file_path argument, otherwise pass NULL).  These are bit fields,
51 *           and can be OR'ed together to log to multiple places.
52 *   abbreviated: If true, capture up to the first 100 lines and last 4K of
53 *           output from the child.  The abbreviated output is not dumped to
54 *           the specified log until the child has exited.
55 *   file_path: if log_target has the LOG_FILE bit set, then this parameter
56 *           must be set to the pathname of the file to log to.
57 *   opts: set to non-NULL if you want to use one or more of the
58 *           FORK_EXECVP_OPTION_* features.
59 *   opts_len: the length of the opts array. When opts is NULL, pass 0.
60 *
61 * Return value:
62 *   0 when logwrap successfully run the child process and captured its status
63 *   -1 when an internal error occurred
64 *   -ECHILD if status is NULL and the child didn't exit properly
65 *   the return value of the child if it exited properly and status is NULL
66 *
67 */
68
69/* Values for the log_target parameter android_fork_execvp_ext() */
70#define LOG_NONE        0
71#define LOG_ALOG        1
72#define LOG_KLOG        2
73#define LOG_FILE        4
74
75/* Write data to child's stdin. */
76#define FORK_EXECVP_OPTION_INPUT             0
77/* Capture data from child's stdout and stderr. */
78#define FORK_EXECVP_OPTION_CAPTURE_OUTPUT    1
79
80struct AndroidForkExecvpOption {
81    int opt_type;
82    union {
83        struct {
84            const uint8_t* input;
85            size_t input_len;
86        } opt_input;
87        struct {
88            void (*on_output)(const uint8_t* /*output*/,
89                              size_t /*output_len*/,
90                              void* /* user_pointer */);
91            void* user_pointer;
92        } opt_capture_output;
93    };
94};
95
96int android_fork_execvp_ext(int argc, char* argv[], int *status, bool ignore_int_quit,
97        int log_target, bool abbreviated, char *file_path,
98        const struct AndroidForkExecvpOption* opts, size_t opts_len);
99
100/* Similar to above, except abbreviated logging is not available, and if logwrap
101 * is true, logging is to the Android system log, and if false, there is no
102 * logging.
103 */
104static inline int android_fork_execvp(int argc, char* argv[], int *status,
105                                     bool ignore_int_quit, bool logwrap)
106{
107    return android_fork_execvp_ext(argc, argv, status, ignore_int_quit,
108                                   (logwrap ? LOG_ALOG : LOG_NONE), false, NULL,
109                                   NULL, 0);
110}
111
112__END_DECLS
113
114#endif /* __LIBS_LOGWRAP_H */
115