]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/cat.c
coccinelle: make use of SYNTHETIC_ERRNO
[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 bool arg_level_prefix = true;
24
25 static int help(void) {
26 _cleanup_free_ char *link = NULL;
27 int r;
28
29 r = terminal_urlify_man("systemd-cat", "1", &link);
30 if (r < 0)
31 return log_oom();
32
33 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
34 "Execute process with stdout/stderr connected to the journal.\n\n"
35 " -h --help Show this help\n"
36 " --version Show package version\n"
37 " -t --identifier=STRING Set syslog identifier\n"
38 " -p --priority=PRIORITY Set priority value (0..7)\n"
39 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
40 "\nSee the %s for details.\n"
41 , program_invocation_short_name
42 , link
43 );
44
45 return 0;
46 }
47
48 static int parse_argv(int argc, char *argv[]) {
49
50 enum {
51 ARG_VERSION = 0x100,
52 ARG_LEVEL_PREFIX
53 };
54
55 static const struct option options[] = {
56 { "help", no_argument, NULL, 'h' },
57 { "version", no_argument, NULL, ARG_VERSION },
58 { "identifier", required_argument, NULL, 't' },
59 { "priority", required_argument, NULL, 'p' },
60 { "level-prefix", required_argument, NULL, ARG_LEVEL_PREFIX },
61 {}
62 };
63
64 int c;
65
66 assert(argc >= 0);
67 assert(argv);
68
69 while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0)
70
71 switch (c) {
72
73 case 'h':
74 help();
75 return 0;
76
77 case ARG_VERSION:
78 return version();
79
80 case 't':
81 if (isempty(optarg))
82 arg_identifier = NULL;
83 else
84 arg_identifier = optarg;
85 break;
86
87 case 'p':
88 arg_priority = log_level_from_string(optarg);
89 if (arg_priority < 0)
90 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
91 "Failed to parse priority value.");
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 static int run(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 return r;
125
126 fd = sd_journal_stream_fd(arg_identifier, arg_priority, arg_level_prefix);
127 if (fd < 0)
128 return log_error_errno(fd, "Failed to create stream fd: %m");
129
130 saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
131
132 r = rearrange_stdio(STDIN_FILENO, fd, fd); /* Invalidates fd on succcess + error! */
133 TAKE_FD(fd);
134 if (r < 0)
135 return log_error_errno(r, "Failed to rearrange stdout/stderr: %m");
136
137 if (argc <= optind)
138 (void) execl("/bin/cat", "/bin/cat", NULL);
139 else
140 (void) execvp(argv[optind], argv + optind);
141 r = -errno;
142
143 /* Let's try to restore a working stderr, so we can print the error message */
144 if (saved_stderr >= 0)
145 (void) dup3(saved_stderr, STDERR_FILENO, 0);
146
147 return log_error_errno(r, "Failed to execute process: %m");
148 }
149
150 DEFINE_MAIN_FUNCTION(run);