1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "sd-journal.h"
12 #include "alloc-util.h"
15 #include "main-func.h"
16 #include "parse-argument.h"
17 #include "parse-util.h"
18 #include "pretty-print.h"
19 #include "string-util.h"
20 #include "syslog-util.h"
21 #include "terminal-util.h"
23 static const char *arg_identifier
= NULL
;
24 static int arg_priority
= LOG_INFO
;
25 static int arg_stderr_priority
= -1;
26 static bool arg_level_prefix
= true;
28 static int help(void) {
29 _cleanup_free_
char *link
= NULL
;
32 r
= terminal_urlify_man("systemd-cat", "1", &link
);
36 printf("%s [OPTIONS...] COMMAND ...\n"
37 "\n%sExecute process with stdout/stderr connected to the journal.%s\n\n"
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"
44 "\nSee the %s for details.\n",
45 program_invocation_short_name
,
53 static int parse_argv(int argc
, char *argv
[]) {
61 static const struct option options
[] = {
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
},
76 while ((c
= getopt_long(argc
, argv
, "+ht:p:", options
, NULL
)) >= 0)
89 arg_identifier
= NULL
;
91 arg_identifier
= optarg
;
95 arg_priority
= log_level_from_string(optarg
);
97 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
98 "Failed to parse priority value.");
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.");
108 case ARG_LEVEL_PREFIX
:
109 r
= parse_boolean_argument("--level-prefix=", optarg
, &arg_level_prefix
);
118 assert_not_reached();
124 static int run(int argc
, char *argv
[]) {
125 _cleanup_close_
int outfd
= -EBADF
, errfd
= -EBADF
, saved_stderr
= -EBADF
;
130 r
= parse_argv(argc
, argv
);
134 outfd
= sd_journal_stream_fd(arg_identifier
, arg_priority
, arg_level_prefix
);
136 return log_error_errno(outfd
, "Failed to create stream fd: %m");
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
);
141 return log_error_errno(errfd
, "Failed to create stream fd: %m");
144 saved_stderr
= fcntl(STDERR_FILENO
, F_DUPFD_CLOEXEC
, 3);
146 r
= rearrange_stdio(STDIN_FILENO
, outfd
, errfd
< 0 ? outfd
: errfd
); /* Invalidates fd on success + error! */
150 return log_error_errno(r
, "Failed to rearrange stdout/stderr: %m");
153 (void) execl("/bin/cat", "/bin/cat", NULL
);
155 (void) execvp(argv
[optind
], argv
+ optind
);
158 /* Let's try to restore a working stderr, so we can print the error message */
159 if (saved_stderr
>= 0)
160 (void) dup3(saved_stderr
, STDERR_FILENO
, 0);
162 return log_error_errno(r
, "Failed to execute process: %m");
165 DEFINE_MAIN_FUNCTION(run
);