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