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