]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/cat.c
src/partition: remove unnecessary uses of "make sure"
[thirdparty/systemd.git] / src / journal / cat.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <getopt.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10
11 #include "sd-journal.h"
12
13 #include "alloc-util.h"
14 #include "build.h"
15 #include "env-util.h"
16 #include "fd-util.h"
17 #include "format-util.h"
18 #include "main-func.h"
19 #include "parse-argument.h"
20 #include "parse-util.h"
21 #include "pretty-print.h"
22 #include "string-util.h"
23 #include "syslog-util.h"
24 #include "terminal-util.h"
25
26 static const char *arg_identifier = NULL;
27 static int arg_priority = LOG_INFO;
28 static int arg_stderr_priority = -1;
29 static bool arg_level_prefix = true;
30
31 static int help(void) {
32 _cleanup_free_ char *link = NULL;
33 int r;
34
35 r = terminal_urlify_man("systemd-cat", "1", &link);
36 if (r < 0)
37 return log_oom();
38
39 printf("%s [OPTIONS...] COMMAND ...\n"
40 "\n%sExecute process with stdout/stderr connected to the journal.%s\n\n"
41 " -h --help Show this help\n"
42 " --version Show package version\n"
43 " -t --identifier=STRING Set syslog identifier\n"
44 " -p --priority=PRIORITY Set priority value (0..7)\n"
45 " --stderr-priority=PRIORITY Set priority value (0..7) used for stderr\n"
46 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
47 "\nSee the %s for details.\n",
48 program_invocation_short_name,
49 ansi_highlight(),
50 ansi_normal(),
51 link);
52
53 return 0;
54 }
55
56 static int parse_argv(int argc, char *argv[]) {
57
58 enum {
59 ARG_VERSION = 0x100,
60 ARG_STDERR_PRIORITY,
61 ARG_LEVEL_PREFIX
62 };
63
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 {}
72 };
73
74 int c, r;
75
76 assert(argc >= 0);
77 assert(argv);
78
79 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
80 * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
81 optind = 0;
82 while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0)
83
84 switch (c) {
85
86 case 'h':
87 help();
88 return 0;
89
90 case ARG_VERSION:
91 return version();
92
93 case 't':
94 if (isempty(optarg))
95 arg_identifier = NULL;
96 else
97 arg_identifier = optarg;
98 break;
99
100 case 'p':
101 arg_priority = log_level_from_string(optarg);
102 if (arg_priority < 0)
103 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
104 "Failed to parse priority value.");
105 break;
106
107 case ARG_STDERR_PRIORITY:
108 arg_stderr_priority = log_level_from_string(optarg);
109 if (arg_stderr_priority < 0)
110 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
111 "Failed to parse stderr priority value.");
112 break;
113
114 case ARG_LEVEL_PREFIX:
115 r = parse_boolean_argument("--level-prefix=", optarg, &arg_level_prefix);
116 if (r < 0)
117 return r;
118 break;
119
120 case '?':
121 return -EINVAL;
122
123 default:
124 assert_not_reached();
125 }
126
127 return 1;
128 }
129
130 static int run(int argc, char *argv[]) {
131 _cleanup_close_ int outfd = -EBADF, errfd = -EBADF, saved_stderr = -EBADF;
132 int r;
133
134 log_setup();
135
136 r = parse_argv(argc, argv);
137 if (r <= 0)
138 return r;
139
140 outfd = sd_journal_stream_fd(arg_identifier, arg_priority, arg_level_prefix);
141 if (outfd < 0)
142 return log_error_errno(outfd, "Failed to create stream fd: %m");
143
144 if (arg_stderr_priority >= 0 && arg_stderr_priority != arg_priority) {
145 errfd = sd_journal_stream_fd(arg_identifier, arg_stderr_priority, arg_level_prefix);
146 if (errfd < 0)
147 return log_error_errno(errfd, "Failed to create stream fd: %m");
148 }
149
150 saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
151
152 r = rearrange_stdio(STDIN_FILENO, outfd, errfd < 0 ? outfd : errfd); /* Invalidates fd on success + error! */
153 TAKE_FD(outfd);
154 TAKE_FD(errfd);
155 if (r < 0)
156 return log_error_errno(r, "Failed to rearrange stdout/stderr: %m");
157
158 if (argc <= optind)
159 (void) execl("/bin/cat", "/bin/cat", NULL);
160 else {
161 struct stat st;
162
163 if (fstat(STDERR_FILENO, &st) < 0)
164 return log_error_errno(errno,
165 "Failed to fstat(%s): %m",
166 FORMAT_PROC_FD_PATH(STDERR_FILENO));
167
168 r = setenvf("JOURNAL_STREAM", /* overwrite = */ true, DEV_FMT ":" INO_FMT, (dev_t) st.st_dev, st.st_ino);
169 if (r < 0)
170 return log_error_errno(r, "Failed to set environment variable JOURNAL_STREAM: %m");
171
172 (void) execvp(argv[optind], argv + optind);
173 }
174 r = -errno;
175
176 /* Let's try to restore a working stderr, so we can print the error message */
177 if (saved_stderr >= 0)
178 (void) dup3(saved_stderr, STDERR_FILENO, 0);
179
180 return log_error_errno(r, "Failed to execute process: %m");
181 }
182
183 DEFINE_MAIN_FUNCTION(run);