1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2012 Lennart Poettering
13 #include "sd-journal.h"
16 #include "parse-util.h"
17 #include "string-util.h"
18 #include "syslog-util.h"
21 static const char *arg_identifier
= NULL
;
22 static int arg_priority
= LOG_INFO
;
23 static bool arg_level_prefix
= true;
25 static void help(void) {
26 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
27 "Execute process with stdout/stderr connected to the journal.\n\n"
28 " -h --help Show this help\n"
29 " --version Show package version\n"
30 " -t --identifier=STRING Set syslog identifier\n"
31 " -p --priority=PRIORITY Set priority value (0..7)\n"
32 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
33 , program_invocation_short_name
);
36 static int parse_argv(int argc
, char *argv
[]) {
43 static const struct option options
[] = {
44 { "help", no_argument
, NULL
, 'h' },
45 { "version", no_argument
, NULL
, ARG_VERSION
},
46 { "identifier", required_argument
, NULL
, 't' },
47 { "priority", required_argument
, NULL
, 'p' },
48 { "level-prefix", required_argument
, NULL
, ARG_LEVEL_PREFIX
},
57 while ((c
= getopt_long(argc
, argv
, "+ht:p:", options
, NULL
)) >= 0)
70 arg_identifier
= NULL
;
72 arg_identifier
= optarg
;
76 arg_priority
= log_level_from_string(optarg
);
77 if (arg_priority
< 0) {
78 log_error("Failed to parse priority value.");
83 case ARG_LEVEL_PREFIX
: {
86 k
= parse_boolean(optarg
);
88 return log_error_errno(k
, "Failed to parse level prefix value.");
98 assert_not_reached("Unhandled option");
104 int main(int argc
, char *argv
[]) {
105 _cleanup_close_
int fd
= -1, saved_stderr
= -1;
108 log_parse_environment();
111 r
= parse_argv(argc
, argv
);
115 fd
= sd_journal_stream_fd(arg_identifier
, arg_priority
, arg_level_prefix
);
117 r
= log_error_errno(fd
, "Failed to create stream fd: %m");
121 saved_stderr
= fcntl(STDERR_FILENO
, F_DUPFD_CLOEXEC
, 3);
123 r
= rearrange_stdio(STDIN_FILENO
, fd
, fd
); /* Invalidates fd on succcess + error! */
126 log_error_errno(r
, "Failed to rearrange stdout/stderr: %m");
131 (void) execl("/bin/cat", "/bin/cat", NULL
);
133 (void) execvp(argv
[optind
], argv
+ optind
);
136 /* Let's try to restore a working stderr, so we can print the error message */
137 if (saved_stderr
>= 0)
138 (void) dup3(saved_stderr
, STDERR_FILENO
, 0);
140 log_error_errno(r
, "Failed to execute process: %m");
143 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;