]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <fcntl.h> | |
5 | #include <getopt.h> | |
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <unistd.h> | |
9 | ||
10 | #include "sd-journal.h" | |
11 | ||
12 | #include "fd-util.h" | |
13 | #include "parse-util.h" | |
14 | #include "string-util.h" | |
15 | #include "syslog-util.h" | |
16 | #include "util.h" | |
17 | ||
18 | static const char *arg_identifier = NULL; | |
19 | static int arg_priority = LOG_INFO; | |
20 | static bool arg_level_prefix = true; | |
21 | ||
22 | static void help(void) { | |
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" | |
29 | " --level-prefix=BOOL Control whether level prefix shall be parsed\n" | |
30 | , program_invocation_short_name); | |
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' }, | |
42 | { "version", no_argument, NULL, ARG_VERSION }, | |
43 | { "identifier", required_argument, NULL, 't' }, | |
44 | { "priority", required_argument, NULL, 'p' }, | |
45 | { "level-prefix", required_argument, NULL, ARG_LEVEL_PREFIX }, | |
46 | {} | |
47 | }; | |
48 | ||
49 | int c; | |
50 | ||
51 | assert(argc >= 0); | |
52 | assert(argv); | |
53 | ||
54 | while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0) | |
55 | ||
56 | switch (c) { | |
57 | ||
58 | case 'h': | |
59 | help(); | |
60 | return 0; | |
61 | ||
62 | case ARG_VERSION: | |
63 | return version(); | |
64 | ||
65 | case 't': | |
66 | if (isempty(optarg)) | |
67 | arg_identifier = NULL; | |
68 | else | |
69 | arg_identifier = optarg; | |
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."); | |
76 | return -EINVAL; | |
77 | } | |
78 | break; | |
79 | ||
80 | case ARG_LEVEL_PREFIX: { | |
81 | int k; | |
82 | ||
83 | k = parse_boolean(optarg); | |
84 | if (k < 0) | |
85 | return log_error_errno(k, "Failed to parse level prefix value."); | |
86 | ||
87 | arg_level_prefix = k; | |
88 | break; | |
89 | } | |
90 | ||
91 | case '?': | |
92 | return -EINVAL; | |
93 | ||
94 | default: | |
95 | assert_not_reached("Unhandled option"); | |
96 | } | |
97 | ||
98 | return 1; | |
99 | } | |
100 | ||
101 | int main(int argc, char *argv[]) { | |
102 | _cleanup_close_ int fd = -1, saved_stderr = -1; | |
103 | int r; | |
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) { | |
114 | r = log_error_errno(fd, "Failed to create stream fd: %m"); | |
115 | goto finish; | |
116 | } | |
117 | ||
118 | saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3); | |
119 | ||
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"); | |
124 | goto finish; | |
125 | } | |
126 | ||
127 | if (argc <= optind) | |
128 | (void) execl("/bin/cat", "/bin/cat", NULL); | |
129 | else | |
130 | (void) execvp(argv[optind], argv + optind); | |
131 | r = -errno; | |
132 | ||
133 | /* Let's try to restore a working stderr, so we can print the error message */ | |
134 | if (saved_stderr >= 0) | |
135 | (void) dup3(saved_stderr, STDERR_FILENO, 0); | |
136 | ||
137 | log_error_errno(r, "Failed to execute process: %m"); | |
138 | ||
139 | finish: | |
140 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
141 | } |