1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "sd-journal.h"
32 #include "parse-util.h"
33 #include "string-util.h"
36 static char *arg_identifier
= NULL
;
37 static int arg_priority
= LOG_INFO
;
38 static bool arg_level_prefix
= true;
40 static void help(void) {
41 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
42 "Execute process with stdout/stderr connected to the journal.\n\n"
43 " -h --help Show this help\n"
44 " --version Show package version\n"
45 " -t --identifier=STRING Set syslog identifier\n"
46 " -p --priority=PRIORITY Set priority value (0..7)\n"
47 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
48 , program_invocation_short_name
);
51 static int parse_argv(int argc
, char *argv
[]) {
58 static const struct option options
[] = {
59 { "help", no_argument
, NULL
, 'h' },
60 { "version", no_argument
, NULL
, ARG_VERSION
},
61 { "identifier", required_argument
, NULL
, 't' },
62 { "priority", required_argument
, NULL
, 'p' },
63 { "level-prefix", required_argument
, NULL
, ARG_LEVEL_PREFIX
},
72 while ((c
= getopt_long(argc
, argv
, "+ht:p:", options
, NULL
)) >= 0)
86 arg_identifier
= NULL
;
88 arg_identifier
= strdup(optarg
);
95 arg_priority
= log_level_from_string(optarg
);
96 if (arg_priority
< 0) {
97 log_error("Failed to parse priority value.");
102 case ARG_LEVEL_PREFIX
: {
105 k
= parse_boolean(optarg
);
107 return log_error_errno(k
, "Failed to parse level prefix value.");
109 arg_level_prefix
= k
;
117 assert_not_reached("Unhandled option");
123 int main(int argc
, char *argv
[]) {
124 _cleanup_close_
int fd
= -1, saved_stderr
= -1;
127 log_parse_environment();
130 r
= parse_argv(argc
, argv
);
134 fd
= sd_journal_stream_fd(arg_identifier
, arg_priority
, arg_level_prefix
);
136 r
= log_error_errno(fd
, "Failed to create stream fd: %m");
140 saved_stderr
= fcntl(STDERR_FILENO
, F_DUPFD_CLOEXEC
, 3);
142 if (dup3(fd
, STDOUT_FILENO
, 0) < 0 ||
143 dup3(fd
, STDERR_FILENO
, 0) < 0) {
144 r
= log_error_errno(errno
, "Failed to duplicate fd: %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 log_error_errno(r
, "Failed to execute process: %m");
165 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;