]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/cat.c
basic/log: add concept of "synthethic errnos"
[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 log_error("Failed to parse priority value.");
91 return -EINVAL;
92 }
93 break;
94
95 case ARG_LEVEL_PREFIX: {
96 int k;
97
98 k = parse_boolean(optarg);
99 if (k < 0)
100 return log_error_errno(k, "Failed to parse level prefix value.");
101
102 arg_level_prefix = k;
103 break;
104 }
105
106 case '?':
107 return -EINVAL;
108
109 default:
110 assert_not_reached("Unhandled option");
111 }
112
113 return 1;
114 }
115
116 static int run(int argc, char *argv[]) {
117 _cleanup_close_ int fd = -1, saved_stderr = -1;
118 int r;
119
120 log_parse_environment();
121 log_open();
122
123 r = parse_argv(argc, argv);
124 if (r <= 0)
125 return r;
126
127 fd = sd_journal_stream_fd(arg_identifier, arg_priority, arg_level_prefix);
128 if (fd < 0)
129 return log_error_errno(fd, "Failed to create stream fd: %m");
130
131 saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
132
133 r = rearrange_stdio(STDIN_FILENO, fd, fd); /* Invalidates fd on succcess + error! */
134 TAKE_FD(fd);
135 if (r < 0)
136 return log_error_errno(r, "Failed to rearrange stdout/stderr: %m");
137
138 if (argc <= optind)
139 (void) execl("/bin/cat", "/bin/cat", NULL);
140 else
141 (void) execvp(argv[optind], argv + optind);
142 r = -errno;
143
144 /* Let's try to restore a working stderr, so we can print the error message */
145 if (saved_stderr >= 0)
146 (void) dup3(saved_stderr, STDERR_FILENO, 0);
147
148 return log_error_errno(r, "Failed to execute process: %m");
149 }
150
151 DEFINE_MAIN_FUNCTION(run);