]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/cat.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / journal / cat.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2012 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <getopt.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12
13 #include "sd-journal.h"
14
15 #include "fd-util.h"
16 #include "parse-util.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 void help(void) {
26 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
27 "Execute process with stdout/stderr connected to the journal.\n\n"
28 " -h --help Show this help\n"
29 " --version Show package version\n"
30 " -t --identifier=STRING Set syslog identifier\n"
31 " -p --priority=PRIORITY Set priority value (0..7)\n"
32 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
33 , program_invocation_short_name);
34 }
35
36 static int parse_argv(int argc, char *argv[]) {
37
38 enum {
39 ARG_VERSION = 0x100,
40 ARG_LEVEL_PREFIX
41 };
42
43 static const struct option options[] = {
44 { "help", no_argument, NULL, 'h' },
45 { "version", no_argument, NULL, ARG_VERSION },
46 { "identifier", required_argument, NULL, 't' },
47 { "priority", required_argument, NULL, 'p' },
48 { "level-prefix", required_argument, NULL, ARG_LEVEL_PREFIX },
49 {}
50 };
51
52 int c;
53
54 assert(argc >= 0);
55 assert(argv);
56
57 while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0)
58
59 switch (c) {
60
61 case 'h':
62 help();
63 return 0;
64
65 case ARG_VERSION:
66 return version();
67
68 case 't':
69 if (isempty(optarg))
70 arg_identifier = NULL;
71 else
72 arg_identifier = optarg;
73 break;
74
75 case 'p':
76 arg_priority = log_level_from_string(optarg);
77 if (arg_priority < 0) {
78 log_error("Failed to parse priority value.");
79 return -EINVAL;
80 }
81 break;
82
83 case ARG_LEVEL_PREFIX: {
84 int k;
85
86 k = parse_boolean(optarg);
87 if (k < 0)
88 return log_error_errno(k, "Failed to parse level prefix value.");
89
90 arg_level_prefix = k;
91 break;
92 }
93
94 case '?':
95 return -EINVAL;
96
97 default:
98 assert_not_reached("Unhandled option");
99 }
100
101 return 1;
102 }
103
104 int main(int argc, char *argv[]) {
105 _cleanup_close_ int fd = -1, saved_stderr = -1;
106 int r;
107
108 log_parse_environment();
109 log_open();
110
111 r = parse_argv(argc, argv);
112 if (r <= 0)
113 goto finish;
114
115 fd = sd_journal_stream_fd(arg_identifier, arg_priority, arg_level_prefix);
116 if (fd < 0) {
117 r = log_error_errno(fd, "Failed to create stream fd: %m");
118 goto finish;
119 }
120
121 saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
122
123 r = rearrange_stdio(STDIN_FILENO, fd, fd); /* Invalidates fd on succcess + error! */
124 fd = -1;
125 if (r < 0) {
126 log_error_errno(r, "Failed to rearrange stdout/stderr: %m");
127 goto finish;
128 }
129
130 if (argc <= optind)
131 (void) execl("/bin/cat", "/bin/cat", NULL);
132 else
133 (void) execvp(argv[optind], argv + optind);
134 r = -errno;
135
136 /* Let's try to restore a working stderr, so we can print the error message */
137 if (saved_stderr >= 0)
138 (void) dup3(saved_stderr, STDERR_FILENO, 0);
139
140 log_error_errno(r, "Failed to execute process: %m");
141
142 finish:
143 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
144 }