1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "sd-journal.h"
12 #include "alloc-util.h"
14 #include "main-func.h"
15 #include "parse-util.h"
16 #include "pretty-print.h"
17 #include "string-util.h"
18 #include "syslog-util.h"
21 static const char *arg_identifier
= NULL
;
22 static int arg_priority
= LOG_INFO
;
23 static int arg_stderr_priority
= -1;
24 static bool arg_level_prefix
= true;
26 static int help(void) {
27 _cleanup_free_
char *link
= NULL
;
30 r
= terminal_urlify_man("systemd-cat", "1", &link
);
34 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
35 "Execute process with stdout/stderr connected to the journal.\n\n"
36 " -h --help Show this help\n"
37 " --version Show package version\n"
38 " -t --identifier=STRING Set syslog identifier\n"
39 " -p --priority=PRIORITY Set priority value (0..7)\n"
40 " --stderr-priority=PRIORITY Set priority value (0..7) used for stderr\n"
41 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
42 "\nSee the %s for details.\n"
43 , program_invocation_short_name
50 static int parse_argv(int argc
, char *argv
[]) {
58 static const struct option options
[] = {
59 { "help", no_argument
, NULL
, 'h' },
60 { "version", no_argument
, NULL
, ARG_VERSION
},
61 { "identifier", required_argument
, NULL
, 't' },
62 { "priority", required_argument
, NULL
, 'p' },
63 { "stderr-priority", required_argument
, NULL
, ARG_STDERR_PRIORITY
},
64 { "level-prefix", required_argument
, NULL
, ARG_LEVEL_PREFIX
},
73 while ((c
= getopt_long(argc
, argv
, "+ht:p:", options
, NULL
)) >= 0)
86 arg_identifier
= NULL
;
88 arg_identifier
= optarg
;
92 arg_priority
= log_level_from_string(optarg
);
94 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
95 "Failed to parse priority value.");
98 case ARG_STDERR_PRIORITY
:
99 arg_stderr_priority
= log_level_from_string(optarg
);
100 if (arg_stderr_priority
< 0)
101 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
102 "Failed to parse stderr priority value.");
105 case ARG_LEVEL_PREFIX
: {
108 k
= parse_boolean(optarg
);
110 return log_error_errno(k
, "Failed to parse level prefix value.");
112 arg_level_prefix
= k
;
120 assert_not_reached("Unhandled option");
126 static int run(int argc
, char *argv
[]) {
127 _cleanup_close_
int outfd
= -1, errfd
= -1, saved_stderr
= -1;
130 log_parse_environment();
133 r
= parse_argv(argc
, argv
);
137 outfd
= sd_journal_stream_fd(arg_identifier
, arg_priority
, arg_level_prefix
);
139 return log_error_errno(outfd
, "Failed to create stream fd: %m");
141 if (arg_stderr_priority
>= 0 && arg_stderr_priority
!= arg_priority
) {
142 errfd
= sd_journal_stream_fd(arg_identifier
, arg_stderr_priority
, arg_level_prefix
);
144 return log_error_errno(errfd
, "Failed to create stream fd: %m");
147 saved_stderr
= fcntl(STDERR_FILENO
, F_DUPFD_CLOEXEC
, 3);
149 r
= rearrange_stdio(STDIN_FILENO
, outfd
, errfd
< 0 ? outfd
: errfd
); /* Invalidates fd on success + error! */
153 return log_error_errno(r
, "Failed to rearrange stdout/stderr: %m");
156 (void) execl("/bin/cat", "/bin/cat", NULL
);
158 (void) execvp(argv
[optind
], argv
+ optind
);
161 /* Let's try to restore a working stderr, so we can print the error message */
162 if (saved_stderr
>= 0)
163 (void) dup3(saved_stderr
, STDERR_FILENO
, 0);
165 return log_error_errno(r
, "Failed to execute process: %m");
168 DEFINE_MAIN_FUNCTION(run
);