1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "sd-journal.h"
11 #include "alloc-util.h"
15 #include "format-util.h"
17 #include "main-func.h"
18 #include "parse-argument.h"
19 #include "pretty-print.h"
20 #include "string-util.h"
21 #include "syslog-util.h"
23 static const char *arg_identifier
= NULL
;
24 static const char *arg_namespace
= NULL
;
25 static int arg_priority
= LOG_INFO
;
26 static int arg_stderr_priority
= -1;
27 static bool arg_level_prefix
= true;
29 static int help(void) {
30 _cleanup_free_
char *link
= NULL
;
33 r
= terminal_urlify_man("systemd-cat", "1", &link
);
37 printf("%s [OPTIONS...] COMMAND ...\n"
38 "\n%sExecute process with stdout/stderr connected to the journal.%s\n\n"
39 " -h --help Show this help\n"
40 " --version Show package version\n"
41 " -t --identifier=STRING Set syslog identifier\n"
42 " -p --priority=PRIORITY Set priority value (0..7)\n"
43 " --stderr-priority=PRIORITY Set priority value (0..7) used for stderr\n"
44 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
45 " --namespace=NAMESPACE Connect to specified journal namespace\n"
46 "\nSee the %s for details.\n",
47 program_invocation_short_name
,
55 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
},
71 { "namespace", required_argument
, NULL
, ARG_NAMESPACE
},
80 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
81 * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
83 while ((c
= getopt_long(argc
, argv
, "+ht:p:", options
, NULL
)) >= 0)
95 arg_identifier
= empty_to_null(optarg
);
99 arg_priority
= log_level_from_string(optarg
);
100 if (arg_priority
< 0)
101 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
102 "Failed to parse priority value.");
105 case ARG_STDERR_PRIORITY
:
106 arg_stderr_priority
= log_level_from_string(optarg
);
107 if (arg_stderr_priority
< 0)
108 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
109 "Failed to parse stderr priority value.");
112 case ARG_LEVEL_PREFIX
:
113 r
= parse_boolean_argument("--level-prefix=", optarg
, &arg_level_prefix
);
119 arg_namespace
= empty_to_null(optarg
);
126 assert_not_reached();
132 static int run(int argc
, char *argv
[]) {
133 _cleanup_close_
int outfd
= -EBADF
, errfd
= -EBADF
, saved_stderr
= -EBADF
;
138 r
= parse_argv(argc
, argv
);
142 outfd
= sd_journal_stream_fd_with_namespace(arg_namespace
, arg_identifier
, arg_priority
, arg_level_prefix
);
144 return log_error_errno(outfd
, "Failed to create stream fd: %m");
146 if (arg_stderr_priority
>= 0 && arg_stderr_priority
!= arg_priority
) {
147 errfd
= sd_journal_stream_fd_with_namespace(arg_namespace
, arg_identifier
, arg_stderr_priority
, arg_level_prefix
);
149 return log_error_errno(errfd
, "Failed to create stream fd: %m");
152 saved_stderr
= fcntl(STDERR_FILENO
, F_DUPFD_CLOEXEC
, 3);
154 r
= rearrange_stdio(STDIN_FILENO
, outfd
, errfd
< 0 ? outfd
: errfd
); /* Invalidates fd on success + error! */
158 return log_error_errno(r
, "Failed to rearrange stdout/stderr: %m");
161 (void) execl("/bin/cat", "/bin/cat", NULL
);
165 if (fstat(STDERR_FILENO
, &st
) < 0)
166 return log_error_errno(errno
,
167 "Failed to fstat(%s): %m",
168 FORMAT_PROC_FD_PATH(STDERR_FILENO
));
170 r
= setenvf("JOURNAL_STREAM", /* overwrite = */ true, DEV_FMT
":" INO_FMT
, (dev_t
) st
.st_dev
, st
.st_ino
);
172 return log_error_errno(r
, "Failed to set environment variable JOURNAL_STREAM: %m");
174 (void) execvp(argv
[optind
], argv
+ optind
);
178 /* Let's try to restore a working stderr, so we can print the error message */
179 if (saved_stderr
>= 0)
180 (void) dup3(saved_stderr
, STDERR_FILENO
, 0);
182 return log_error_errno(r
, "Failed to execute process: %m");
185 DEFINE_MAIN_FUNCTION(run
);