]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
755a02c6 | 2 | |
755a02c6 | 3 | #include <errno.h> |
abad76cc | 4 | #include <fcntl.h> |
3f6fd1ba LP |
5 | #include <getopt.h> |
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <unistd.h> | |
755a02c6 | 9 | |
3f6fd1ba | 10 | #include "sd-journal.h" |
755a02c6 | 11 | |
3ffd4af2 | 12 | #include "fd-util.h" |
6bedfcbb | 13 | #include "parse-util.h" |
07630cea | 14 | #include "string-util.h" |
7ccbd1ae | 15 | #include "syslog-util.h" |
755a02c6 | 16 | #include "util.h" |
755a02c6 | 17 | |
2afcd690 | 18 | static const char *arg_identifier = NULL; |
d508ac0b | 19 | static int arg_priority = LOG_INFO; |
755a02c6 LP |
20 | static bool arg_level_prefix = true; |
21 | ||
601185b4 | 22 | static void help(void) { |
755a02c6 LP |
23 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
24 | "Execute process with stdout/stderr connected to the journal.\n\n" | |
25 | " -h --help Show this help\n" | |
26 | " --version Show package version\n" | |
27 | " -t --identifier=STRING Set syslog identifier\n" | |
28 | " -p --priority=PRIORITY Set priority value (0..7)\n" | |
601185b4 ZJS |
29 | " --level-prefix=BOOL Control whether level prefix shall be parsed\n" |
30 | , program_invocation_short_name); | |
755a02c6 LP |
31 | } |
32 | ||
33 | static int parse_argv(int argc, char *argv[]) { | |
34 | ||
35 | enum { | |
36 | ARG_VERSION = 0x100, | |
37 | ARG_LEVEL_PREFIX | |
38 | }; | |
39 | ||
40 | static const struct option options[] = { | |
41 | { "help", no_argument, NULL, 'h' }, | |
9aac0b2c | 42 | { "version", no_argument, NULL, ARG_VERSION }, |
755a02c6 LP |
43 | { "identifier", required_argument, NULL, 't' }, |
44 | { "priority", required_argument, NULL, 'p' }, | |
45 | { "level-prefix", required_argument, NULL, ARG_LEVEL_PREFIX }, | |
eb9da376 | 46 | {} |
755a02c6 LP |
47 | }; |
48 | ||
49 | int c; | |
50 | ||
51 | assert(argc >= 0); | |
52 | assert(argv); | |
53 | ||
601185b4 | 54 | while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0) |
755a02c6 LP |
55 | |
56 | switch (c) { | |
57 | ||
58 | case 'h': | |
601185b4 ZJS |
59 | help(); |
60 | return 0; | |
755a02c6 LP |
61 | |
62 | case ARG_VERSION: | |
3f6fd1ba | 63 | return version(); |
755a02c6 LP |
64 | |
65 | case 't': | |
755a02c6 LP |
66 | if (isempty(optarg)) |
67 | arg_identifier = NULL; | |
2afcd690 LP |
68 | else |
69 | arg_identifier = optarg; | |
755a02c6 LP |
70 | break; |
71 | ||
72 | case 'p': | |
73 | arg_priority = log_level_from_string(optarg); | |
74 | if (arg_priority < 0) { | |
75 | log_error("Failed to parse priority value."); | |
e4603df5 | 76 | return -EINVAL; |
755a02c6 LP |
77 | } |
78 | break; | |
79 | ||
80 | case ARG_LEVEL_PREFIX: { | |
81 | int k; | |
82 | ||
83 | k = parse_boolean(optarg); | |
939c173f LP |
84 | if (k < 0) |
85 | return log_error_errno(k, "Failed to parse level prefix value."); | |
86 | ||
755a02c6 LP |
87 | arg_level_prefix = k; |
88 | break; | |
89 | } | |
90 | ||
eb9da376 | 91 | case '?': |
755a02c6 | 92 | return -EINVAL; |
eb9da376 LP |
93 | |
94 | default: | |
95 | assert_not_reached("Unhandled option"); | |
755a02c6 | 96 | } |
755a02c6 LP |
97 | |
98 | return 1; | |
99 | } | |
100 | ||
101 | int main(int argc, char *argv[]) { | |
939c173f LP |
102 | _cleanup_close_ int fd = -1, saved_stderr = -1; |
103 | int r; | |
755a02c6 LP |
104 | |
105 | log_parse_environment(); | |
106 | log_open(); | |
107 | ||
108 | r = parse_argv(argc, argv); | |
109 | if (r <= 0) | |
110 | goto finish; | |
111 | ||
112 | fd = sd_journal_stream_fd(arg_identifier, arg_priority, arg_level_prefix); | |
113 | if (fd < 0) { | |
939c173f | 114 | r = log_error_errno(fd, "Failed to create stream fd: %m"); |
755a02c6 LP |
115 | goto finish; |
116 | } | |
117 | ||
118 | saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3); | |
119 | ||
2b33ab09 LP |
120 | r = rearrange_stdio(STDIN_FILENO, fd, fd); /* Invalidates fd on succcess + error! */ |
121 | fd = -1; | |
122 | if (r < 0) { | |
123 | log_error_errno(r, "Failed to rearrange stdout/stderr: %m"); | |
755a02c6 LP |
124 | goto finish; |
125 | } | |
126 | ||
755a02c6 | 127 | if (argc <= optind) |
939c173f | 128 | (void) execl("/bin/cat", "/bin/cat", NULL); |
755a02c6 | 129 | else |
939c173f | 130 | (void) execvp(argv[optind], argv + optind); |
9aac0b2c LP |
131 | r = -errno; |
132 | ||
755a02c6 LP |
133 | /* Let's try to restore a working stderr, so we can print the error message */ |
134 | if (saved_stderr >= 0) | |
939c173f | 135 | (void) dup3(saved_stderr, STDERR_FILENO, 0); |
755a02c6 | 136 | |
da927ba9 | 137 | log_error_errno(r, "Failed to execute process: %m"); |
755a02c6 LP |
138 | |
139 | finish: | |
755a02c6 LP |
140 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
141 | } |