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