1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "sd-journal.h"
12 #include "alloc-util.h"
14 #include "parse-util.h"
15 #include "string-util.h"
16 #include "syslog-util.h"
18 #include "terminal-util.h"
20 static const char *arg_identifier
= NULL
;
21 static int arg_priority
= LOG_INFO
;
22 static bool arg_level_prefix
= true;
24 static int help(void) {
25 _cleanup_free_
char *link
= NULL
;
28 r
= terminal_urlify_man("systemd-cat", "1", &link
);
32 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
33 "Execute process with stdout/stderr connected to the journal.\n\n"
34 " -h --help Show this help\n"
35 " --version Show package version\n"
36 " -t --identifier=STRING Set syslog identifier\n"
37 " -p --priority=PRIORITY Set priority value (0..7)\n"
38 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
39 "\nSee the %s for details.\n"
40 , program_invocation_short_name
47 static int parse_argv(int argc
, char *argv
[]) {
54 static const struct option options
[] = {
55 { "help", no_argument
, NULL
, 'h' },
56 { "version", no_argument
, NULL
, ARG_VERSION
},
57 { "identifier", required_argument
, NULL
, 't' },
58 { "priority", required_argument
, NULL
, 'p' },
59 { "level-prefix", required_argument
, NULL
, ARG_LEVEL_PREFIX
},
68 while ((c
= getopt_long(argc
, argv
, "+ht:p:", options
, NULL
)) >= 0)
81 arg_identifier
= NULL
;
83 arg_identifier
= optarg
;
87 arg_priority
= log_level_from_string(optarg
);
88 if (arg_priority
< 0) {
89 log_error("Failed to parse priority value.");
94 case ARG_LEVEL_PREFIX
: {
97 k
= parse_boolean(optarg
);
99 return log_error_errno(k
, "Failed to parse level prefix value.");
101 arg_level_prefix
= k
;
109 assert_not_reached("Unhandled option");
115 static int run(int argc
, char *argv
[]) {
116 _cleanup_close_
int fd
= -1, saved_stderr
= -1;
119 log_parse_environment();
122 r
= parse_argv(argc
, argv
);
126 fd
= sd_journal_stream_fd(arg_identifier
, arg_priority
, arg_level_prefix
);
128 return log_error_errno(fd
, "Failed to create stream fd: %m");
130 saved_stderr
= fcntl(STDERR_FILENO
, F_DUPFD_CLOEXEC
, 3);
132 r
= rearrange_stdio(STDIN_FILENO
, fd
, fd
); /* Invalidates fd on succcess + error! */
135 return log_error_errno(r
, "Failed to rearrange stdout/stderr: %m");
138 (void) execl("/bin/cat", "/bin/cat", NULL
);
140 (void) execvp(argv
[optind
], argv
+ optind
);
143 /* Let's try to restore a working stderr, so we can print the error message */
144 if (saved_stderr
>= 0)
145 (void) dup3(saved_stderr
, STDERR_FILENO
, 0);
147 return log_error_errno(r
, "Failed to execute process: %m");
150 DEFINE_MAIN_FUNCTION(run
);