]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/cat.c
404a74873634d33696467870fd375c50328631d3
[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 <unistd.h>
9
10 #include "sd-journal.h"
11
12 #include "alloc-util.h"
13 #include "build.h"
14 #include "fd-util.h"
15 #include "main-func.h"
16 #include "parse-argument.h"
17 #include "parse-util.h"
18 #include "pretty-print.h"
19 #include "string-util.h"
20 #include "syslog-util.h"
21 #include "terminal-util.h"
22
23 static const char *arg_identifier = NULL;
24 static int arg_priority = LOG_INFO;
25 static int arg_stderr_priority = -1;
26 static bool arg_level_prefix = true;
27
28 static int help(void) {
29 _cleanup_free_ char *link = NULL;
30 int r;
31
32 r = terminal_urlify_man("systemd-cat", "1", &link);
33 if (r < 0)
34 return log_oom();
35
36 printf("%s [OPTIONS...] COMMAND ...\n"
37 "\n%sExecute process with stdout/stderr connected to the journal.%s\n\n"
38 " -h --help Show this help\n"
39 " --version Show package version\n"
40 " -t --identifier=STRING Set syslog identifier\n"
41 " -p --priority=PRIORITY Set priority value (0..7)\n"
42 " --stderr-priority=PRIORITY Set priority value (0..7) used for stderr\n"
43 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
44 "\nSee the %s for details.\n",
45 program_invocation_short_name,
46 ansi_highlight(),
47 ansi_normal(),
48 link);
49
50 return 0;
51 }
52
53 static int parse_argv(int argc, char *argv[]) {
54
55 enum {
56 ARG_VERSION = 0x100,
57 ARG_STDERR_PRIORITY,
58 ARG_LEVEL_PREFIX
59 };
60
61 static const struct option options[] = {
62 { "help", no_argument, NULL, 'h' },
63 { "version", no_argument, NULL, ARG_VERSION },
64 { "identifier", required_argument, NULL, 't' },
65 { "priority", required_argument, NULL, 'p' },
66 { "stderr-priority", required_argument, NULL, ARG_STDERR_PRIORITY },
67 { "level-prefix", required_argument, NULL, ARG_LEVEL_PREFIX },
68 {}
69 };
70
71 int c, r;
72
73 assert(argc >= 0);
74 assert(argv);
75
76 while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0)
77
78 switch (c) {
79
80 case 'h':
81 help();
82 return 0;
83
84 case ARG_VERSION:
85 return version();
86
87 case 't':
88 if (isempty(optarg))
89 arg_identifier = NULL;
90 else
91 arg_identifier = optarg;
92 break;
93
94 case 'p':
95 arg_priority = log_level_from_string(optarg);
96 if (arg_priority < 0)
97 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
98 "Failed to parse priority value.");
99 break;
100
101 case ARG_STDERR_PRIORITY:
102 arg_stderr_priority = log_level_from_string(optarg);
103 if (arg_stderr_priority < 0)
104 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
105 "Failed to parse stderr priority value.");
106 break;
107
108 case ARG_LEVEL_PREFIX:
109 r = parse_boolean_argument("--level-prefix=", optarg, &arg_level_prefix);
110 if (r < 0)
111 return r;
112 break;
113
114 case '?':
115 return -EINVAL;
116
117 default:
118 assert_not_reached();
119 }
120
121 return 1;
122 }
123
124 static int run(int argc, char *argv[]) {
125 _cleanup_close_ int outfd = -1, errfd = -1, saved_stderr = -1;
126 int r;
127
128 log_setup();
129
130 r = parse_argv(argc, argv);
131 if (r <= 0)
132 return r;
133
134 outfd = sd_journal_stream_fd(arg_identifier, arg_priority, arg_level_prefix);
135 if (outfd < 0)
136 return log_error_errno(outfd, "Failed to create stream fd: %m");
137
138 if (arg_stderr_priority >= 0 && arg_stderr_priority != arg_priority) {
139 errfd = sd_journal_stream_fd(arg_identifier, arg_stderr_priority, arg_level_prefix);
140 if (errfd < 0)
141 return log_error_errno(errfd, "Failed to create stream fd: %m");
142 }
143
144 saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
145
146 r = rearrange_stdio(STDIN_FILENO, outfd, errfd < 0 ? outfd : errfd); /* Invalidates fd on success + error! */
147 TAKE_FD(outfd);
148 TAKE_FD(errfd);
149 if (r < 0)
150 return log_error_errno(r, "Failed to rearrange stdout/stderr: %m");
151
152 if (argc <= optind)
153 (void) execl("/bin/cat", "/bin/cat", NULL);
154 else
155 (void) execvp(argv[optind], argv + optind);
156 r = -errno;
157
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);
161
162 return log_error_errno(r, "Failed to execute process: %m");
163 }
164
165 DEFINE_MAIN_FUNCTION(run);