1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "sd-journal.h"
13 #include "alloc-util.h"
17 #include "format-util.h"
18 #include "main-func.h"
19 #include "parse-argument.h"
20 #include "parse-util.h"
21 #include "pretty-print.h"
22 #include "string-util.h"
23 #include "syslog-util.h"
24 #include "terminal-util.h"
26 static const char *arg_identifier
= NULL
;
27 static int arg_priority
= LOG_INFO
;
28 static int arg_stderr_priority
= -1;
29 static bool arg_level_prefix
= true;
31 static int help(void) {
32 _cleanup_free_
char *link
= NULL
;
35 r
= terminal_urlify_man("systemd-cat", "1", &link
);
39 printf("%s [OPTIONS...] COMMAND ...\n"
40 "\n%sExecute process with stdout/stderr connected to the journal.%s\n\n"
41 " -h --help Show this help\n"
42 " --version Show package version\n"
43 " -t --identifier=STRING Set syslog identifier\n"
44 " -p --priority=PRIORITY Set priority value (0..7)\n"
45 " --stderr-priority=PRIORITY Set priority value (0..7) used for stderr\n"
46 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
47 "\nSee the %s for details.\n",
48 program_invocation_short_name
,
56 static int parse_argv(int argc
, char *argv
[]) {
64 static const struct option options
[] = {
65 { "help", no_argument
, NULL
, 'h' },
66 { "version", no_argument
, NULL
, ARG_VERSION
},
67 { "identifier", required_argument
, NULL
, 't' },
68 { "priority", required_argument
, NULL
, 'p' },
69 { "stderr-priority", required_argument
, NULL
, ARG_STDERR_PRIORITY
},
70 { "level-prefix", required_argument
, NULL
, ARG_LEVEL_PREFIX
},
79 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
80 * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
82 while ((c
= getopt_long(argc
, argv
, "+ht:p:", options
, NULL
)) >= 0)
95 arg_identifier
= NULL
;
97 arg_identifier
= optarg
;
101 arg_priority
= log_level_from_string(optarg
);
102 if (arg_priority
< 0)
103 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
104 "Failed to parse priority value.");
107 case ARG_STDERR_PRIORITY
:
108 arg_stderr_priority
= log_level_from_string(optarg
);
109 if (arg_stderr_priority
< 0)
110 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
111 "Failed to parse stderr priority value.");
114 case ARG_LEVEL_PREFIX
:
115 r
= parse_boolean_argument("--level-prefix=", optarg
, &arg_level_prefix
);
124 assert_not_reached();
130 static int run(int argc
, char *argv
[]) {
131 _cleanup_close_
int outfd
= -EBADF
, errfd
= -EBADF
, saved_stderr
= -EBADF
;
136 r
= parse_argv(argc
, argv
);
140 outfd
= sd_journal_stream_fd(arg_identifier
, arg_priority
, arg_level_prefix
);
142 return log_error_errno(outfd
, "Failed to create stream fd: %m");
144 if (arg_stderr_priority
>= 0 && arg_stderr_priority
!= arg_priority
) {
145 errfd
= sd_journal_stream_fd(arg_identifier
, arg_stderr_priority
, arg_level_prefix
);
147 return log_error_errno(errfd
, "Failed to create stream fd: %m");
150 saved_stderr
= fcntl(STDERR_FILENO
, F_DUPFD_CLOEXEC
, 3);
152 r
= rearrange_stdio(STDIN_FILENO
, outfd
, errfd
< 0 ? outfd
: errfd
); /* Invalidates fd on success + error! */
156 return log_error_errno(r
, "Failed to rearrange stdout/stderr: %m");
159 (void) execl("/bin/cat", "/bin/cat", NULL
);
163 if (fstat(STDERR_FILENO
, &st
) < 0)
164 return log_error_errno(errno
,
165 "Failed to fstat(%s): %m",
166 FORMAT_PROC_FD_PATH(STDERR_FILENO
));
168 r
= setenvf("JOURNAL_STREAM", /* overwrite = */ true, DEV_FMT
":" INO_FMT
, (dev_t
) st
.st_dev
, st
.st_ino
);
170 return log_error_errno(r
, "Failed to set environment variable JOURNAL_STREAM: %m");
172 (void) execvp(argv
[optind
], argv
+ optind
);
176 /* Let's try to restore a working stderr, so we can print the error message */
177 if (saved_stderr
>= 0)
178 (void) dup3(saved_stderr
, STDERR_FILENO
, 0);
180 return log_error_errno(r
, "Failed to execute process: %m");
183 DEFINE_MAIN_FUNCTION(run
);