1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
19 #include "terminal-util.h"
22 static const char *arg_identifier
= NULL
;
23 static int arg_priority
= LOG_INFO
;
24 static int arg_stderr_priority
= -1;
25 static bool arg_level_prefix
= true;
27 static int help(void) {
28 _cleanup_free_
char *link
= NULL
;
31 r
= terminal_urlify_man("systemd-cat", "1", &link
);
35 printf("%s [OPTIONS...] COMMAND ...\n"
36 "\n%sExecute process with stdout/stderr connected to the journal.%s\n\n"
37 " -h --help Show this help\n"
38 " --version Show package version\n"
39 " -t --identifier=STRING Set syslog identifier\n"
40 " -p --priority=PRIORITY Set priority value (0..7)\n"
41 " --stderr-priority=PRIORITY Set priority value (0..7) used for stderr\n"
42 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
43 "\nSee the %s for details.\n"
44 , program_invocation_short_name
45 , ansi_highlight(), ansi_normal()
52 static int parse_argv(int argc
, char *argv
[]) {
60 static const struct option options
[] = {
61 { "help", no_argument
, NULL
, 'h' },
62 { "version", no_argument
, NULL
, ARG_VERSION
},
63 { "identifier", required_argument
, NULL
, 't' },
64 { "priority", required_argument
, NULL
, 'p' },
65 { "stderr-priority", required_argument
, NULL
, ARG_STDERR_PRIORITY
},
66 { "level-prefix", required_argument
, NULL
, ARG_LEVEL_PREFIX
},
75 while ((c
= getopt_long(argc
, argv
, "+ht:p:", options
, NULL
)) >= 0)
88 arg_identifier
= NULL
;
90 arg_identifier
= optarg
;
94 arg_priority
= log_level_from_string(optarg
);
96 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
97 "Failed to parse priority value.");
100 case ARG_STDERR_PRIORITY
:
101 arg_stderr_priority
= log_level_from_string(optarg
);
102 if (arg_stderr_priority
< 0)
103 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
104 "Failed to parse stderr priority value.");
107 case ARG_LEVEL_PREFIX
: {
110 k
= parse_boolean(optarg
);
112 return log_error_errno(k
, "Failed to parse level prefix value.");
114 arg_level_prefix
= k
;
122 assert_not_reached("Unhandled option");
128 static int run(int argc
, char *argv
[]) {
129 _cleanup_close_
int outfd
= -1, errfd
= -1, saved_stderr
= -1;
134 r
= parse_argv(argc
, argv
);
138 outfd
= sd_journal_stream_fd(arg_identifier
, arg_priority
, arg_level_prefix
);
140 return log_error_errno(outfd
, "Failed to create stream fd: %m");
142 if (arg_stderr_priority
>= 0 && arg_stderr_priority
!= arg_priority
) {
143 errfd
= sd_journal_stream_fd(arg_identifier
, arg_stderr_priority
, arg_level_prefix
);
145 return log_error_errno(errfd
, "Failed to create stream fd: %m");
148 saved_stderr
= fcntl(STDERR_FILENO
, F_DUPFD_CLOEXEC
, 3);
150 r
= rearrange_stdio(STDIN_FILENO
, outfd
, errfd
< 0 ? outfd
: errfd
); /* Invalidates fd on success + error! */
154 return log_error_errno(r
, "Failed to rearrange stdout/stderr: %m");
157 (void) execl("/bin/cat", "/bin/cat", NULL
);
159 (void) execvp(argv
[optind
], argv
+ optind
);
162 /* Let's try to restore a working stderr, so we can print the error message */
163 if (saved_stderr
>= 0)
164 (void) dup3(saved_stderr
, STDERR_FILENO
, 0);
166 return log_error_errno(r
, "Failed to execute process: %m");
169 DEFINE_MAIN_FUNCTION(run
);