]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
755a02c6 | 2 | |
abad76cc | 3 | #include <fcntl.h> |
3f6fd1ba LP |
4 | #include <getopt.h> |
5 | #include <stdio.h> | |
0688bea1 | 6 | #include <sys/stat.h> |
3f6fd1ba | 7 | #include <unistd.h> |
755a02c6 | 8 | |
3f6fd1ba | 9 | #include "sd-journal.h" |
755a02c6 | 10 | |
37ec0fdd | 11 | #include "alloc-util.h" |
d6b4d1c7 | 12 | #include "build.h" |
b20e9dc5 | 13 | #include "env-util.h" |
3ffd4af2 | 14 | #include "fd-util.h" |
0688bea1 | 15 | #include "format-util.h" |
93a1f792 | 16 | #include "log.h" |
5e332028 | 17 | #include "main-func.h" |
599c7c54 | 18 | #include "parse-argument.h" |
294bf0c3 | 19 | #include "pretty-print.h" |
07630cea | 20 | #include "string-util.h" |
7ccbd1ae | 21 | #include "syslog-util.h" |
755a02c6 | 22 | |
2afcd690 | 23 | static const char *arg_identifier = NULL; |
45bcab66 | 24 | static const char *arg_namespace = NULL; |
d508ac0b | 25 | static int arg_priority = LOG_INFO; |
a08c3e8f | 26 | static int arg_stderr_priority = -1; |
755a02c6 LP |
27 | static bool arg_level_prefix = true; |
28 | ||
37ec0fdd LP |
29 | static int help(void) { |
30 | _cleanup_free_ char *link = NULL; | |
31 | int r; | |
32 | ||
33 | r = terminal_urlify_man("systemd-cat", "1", &link); | |
34 | if (r < 0) | |
35 | return log_oom(); | |
36 | ||
353b2baa LP |
37 | printf("%s [OPTIONS...] COMMAND ...\n" |
38 | "\n%sExecute process with stdout/stderr connected to the journal.%s\n\n" | |
a08c3e8f MS |
39 | " -h --help Show this help\n" |
40 | " --version Show package version\n" | |
41 | " -t --identifier=STRING Set syslog identifier\n" | |
42 | " -p --priority=PRIORITY Set priority value (0..7)\n" | |
43 | " --stderr-priority=PRIORITY Set priority value (0..7) used for stderr\n" | |
44 | " --level-prefix=BOOL Control whether level prefix shall be parsed\n" | |
45bcab66 | 45 | " --namespace=NAMESPACE Connect to specified journal namespace\n" |
bc556335 DDM |
46 | "\nSee the %s for details.\n", |
47 | program_invocation_short_name, | |
48 | ansi_highlight(), | |
49 | ansi_normal(), | |
50 | link); | |
37ec0fdd LP |
51 | |
52 | return 0; | |
755a02c6 LP |
53 | } |
54 | ||
55 | static int parse_argv(int argc, char *argv[]) { | |
56 | ||
57 | enum { | |
58 | ARG_VERSION = 0x100, | |
a08c3e8f | 59 | ARG_STDERR_PRIORITY, |
45bcab66 MY |
60 | ARG_LEVEL_PREFIX, |
61 | ARG_NAMESPACE, | |
755a02c6 LP |
62 | }; |
63 | ||
64 | static const struct option options[] = { | |
a08c3e8f MS |
65 | { "help", no_argument, NULL, 'h' }, |
66 | { "version", no_argument, NULL, ARG_VERSION }, | |
67 | { "identifier", required_argument, NULL, 't' }, | |
68 | { "priority", required_argument, NULL, 'p' }, | |
69 | { "stderr-priority", required_argument, NULL, ARG_STDERR_PRIORITY }, | |
70 | { "level-prefix", required_argument, NULL, ARG_LEVEL_PREFIX }, | |
45bcab66 | 71 | { "namespace", required_argument, NULL, ARG_NAMESPACE }, |
eb9da376 | 72 | {} |
755a02c6 LP |
73 | }; |
74 | ||
599c7c54 | 75 | int c, r; |
755a02c6 LP |
76 | |
77 | assert(argc >= 0); | |
78 | assert(argv); | |
79 | ||
ef9c12b1 YW |
80 | /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() |
81 | * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ | |
82 | optind = 0; | |
601185b4 | 83 | while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0) |
755a02c6 LP |
84 | |
85 | switch (c) { | |
86 | ||
87 | case 'h': | |
601185b4 ZJS |
88 | help(); |
89 | return 0; | |
755a02c6 LP |
90 | |
91 | case ARG_VERSION: | |
3f6fd1ba | 92 | return version(); |
755a02c6 LP |
93 | |
94 | case 't': | |
80fcdb73 | 95 | arg_identifier = empty_to_null(optarg); |
755a02c6 LP |
96 | break; |
97 | ||
98 | case 'p': | |
99 | arg_priority = log_level_from_string(optarg); | |
baaa35ad ZJS |
100 | if (arg_priority < 0) |
101 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
102 | "Failed to parse priority value."); | |
755a02c6 LP |
103 | break; |
104 | ||
a08c3e8f MS |
105 | case ARG_STDERR_PRIORITY: |
106 | arg_stderr_priority = log_level_from_string(optarg); | |
107 | if (arg_stderr_priority < 0) | |
108 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
109 | "Failed to parse stderr priority value."); | |
110 | break; | |
111 | ||
599c7c54 ZJS |
112 | case ARG_LEVEL_PREFIX: |
113 | r = parse_boolean_argument("--level-prefix=", optarg, &arg_level_prefix); | |
114 | if (r < 0) | |
115 | return r; | |
755a02c6 | 116 | break; |
755a02c6 | 117 | |
45bcab66 | 118 | case ARG_NAMESPACE: |
80fcdb73 | 119 | arg_namespace = empty_to_null(optarg); |
45bcab66 MY |
120 | break; |
121 | ||
eb9da376 | 122 | case '?': |
755a02c6 | 123 | return -EINVAL; |
eb9da376 LP |
124 | |
125 | default: | |
04499a70 | 126 | assert_not_reached(); |
755a02c6 | 127 | } |
755a02c6 LP |
128 | |
129 | return 1; | |
130 | } | |
131 | ||
634a0ad3 | 132 | static int run(int argc, char *argv[]) { |
254d1313 | 133 | _cleanup_close_ int outfd = -EBADF, errfd = -EBADF, saved_stderr = -EBADF; |
939c173f | 134 | int r; |
755a02c6 | 135 | |
d2acb93d | 136 | log_setup(); |
755a02c6 LP |
137 | |
138 | r = parse_argv(argc, argv); | |
139 | if (r <= 0) | |
634a0ad3 | 140 | return r; |
755a02c6 | 141 | |
45bcab66 | 142 | outfd = sd_journal_stream_fd_with_namespace(arg_namespace, arg_identifier, arg_priority, arg_level_prefix); |
a08c3e8f MS |
143 | if (outfd < 0) |
144 | return log_error_errno(outfd, "Failed to create stream fd: %m"); | |
145 | ||
146 | if (arg_stderr_priority >= 0 && arg_stderr_priority != arg_priority) { | |
45bcab66 | 147 | errfd = sd_journal_stream_fd_with_namespace(arg_namespace, arg_identifier, arg_stderr_priority, arg_level_prefix); |
a08c3e8f MS |
148 | if (errfd < 0) |
149 | return log_error_errno(errfd, "Failed to create stream fd: %m"); | |
150 | } | |
755a02c6 LP |
151 | |
152 | saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3); | |
153 | ||
5238e957 | 154 | r = rearrange_stdio(STDIN_FILENO, outfd, errfd < 0 ? outfd : errfd); /* Invalidates fd on success + error! */ |
a08c3e8f MS |
155 | TAKE_FD(outfd); |
156 | TAKE_FD(errfd); | |
634a0ad3 ZJS |
157 | if (r < 0) |
158 | return log_error_errno(r, "Failed to rearrange stdout/stderr: %m"); | |
755a02c6 | 159 | |
755a02c6 | 160 | if (argc <= optind) |
939c173f | 161 | (void) execl("/bin/cat", "/bin/cat", NULL); |
0688bea1 | 162 | else { |
0688bea1 MY |
163 | struct stat st; |
164 | ||
165 | if (fstat(STDERR_FILENO, &st) < 0) | |
166 | return log_error_errno(errno, | |
167 | "Failed to fstat(%s): %m", | |
168 | FORMAT_PROC_FD_PATH(STDERR_FILENO)); | |
169 | ||
b20e9dc5 LP |
170 | r = setenvf("JOURNAL_STREAM", /* overwrite = */ true, DEV_FMT ":" INO_FMT, (dev_t) st.st_dev, st.st_ino); |
171 | if (r < 0) | |
172 | return log_error_errno(r, "Failed to set environment variable JOURNAL_STREAM: %m"); | |
0688bea1 | 173 | |
939c173f | 174 | (void) execvp(argv[optind], argv + optind); |
0688bea1 | 175 | } |
9aac0b2c LP |
176 | r = -errno; |
177 | ||
755a02c6 LP |
178 | /* Let's try to restore a working stderr, so we can print the error message */ |
179 | if (saved_stderr >= 0) | |
939c173f | 180 | (void) dup3(saved_stderr, STDERR_FILENO, 0); |
755a02c6 | 181 | |
634a0ad3 | 182 | return log_error_errno(r, "Failed to execute process: %m"); |
755a02c6 | 183 | } |
634a0ad3 ZJS |
184 | |
185 | DEFINE_MAIN_FUNCTION(run); |