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