]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/cat.c
Merge pull request #1983 from dmedri/master
[thirdparty/systemd.git] / src / journal / cat.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2012 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28
29 #include "sd-journal.h"
30
31 #include "fd-util.h"
32 #include "parse-util.h"
33 #include "string-util.h"
34 #include "syslog-util.h"
35 #include "util.h"
36
37 static char *arg_identifier = NULL;
38 static int arg_priority = LOG_INFO;
39 static bool arg_level_prefix = true;
40
41 static void help(void) {
42 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
43 "Execute process with stdout/stderr connected to the journal.\n\n"
44 " -h --help Show this help\n"
45 " --version Show package version\n"
46 " -t --identifier=STRING Set syslog identifier\n"
47 " -p --priority=PRIORITY Set priority value (0..7)\n"
48 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
49 , program_invocation_short_name);
50 }
51
52 static int parse_argv(int argc, char *argv[]) {
53
54 enum {
55 ARG_VERSION = 0x100,
56 ARG_LEVEL_PREFIX
57 };
58
59 static const struct option options[] = {
60 { "help", no_argument, NULL, 'h' },
61 { "version", no_argument, NULL, ARG_VERSION },
62 { "identifier", required_argument, NULL, 't' },
63 { "priority", required_argument, NULL, 'p' },
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 free(arg_identifier);
86 if (isempty(optarg))
87 arg_identifier = NULL;
88 else {
89 arg_identifier = strdup(optarg);
90 if (!arg_identifier)
91 return log_oom();
92 }
93 break;
94
95 case 'p':
96 arg_priority = log_level_from_string(optarg);
97 if (arg_priority < 0) {
98 log_error("Failed to parse priority value.");
99 return -EINVAL;
100 }
101 break;
102
103 case ARG_LEVEL_PREFIX: {
104 int k;
105
106 k = parse_boolean(optarg);
107 if (k < 0)
108 return log_error_errno(k, "Failed to parse level prefix value.");
109
110 arg_level_prefix = k;
111 break;
112 }
113
114 case '?':
115 return -EINVAL;
116
117 default:
118 assert_not_reached("Unhandled option");
119 }
120
121 return 1;
122 }
123
124 int main(int argc, char *argv[]) {
125 _cleanup_close_ int fd = -1, saved_stderr = -1;
126 int r;
127
128 log_parse_environment();
129 log_open();
130
131 r = parse_argv(argc, argv);
132 if (r <= 0)
133 goto finish;
134
135 fd = sd_journal_stream_fd(arg_identifier, arg_priority, arg_level_prefix);
136 if (fd < 0) {
137 r = log_error_errno(fd, "Failed to create stream fd: %m");
138 goto finish;
139 }
140
141 saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
142
143 if (dup3(fd, STDOUT_FILENO, 0) < 0 ||
144 dup3(fd, STDERR_FILENO, 0) < 0) {
145 r = log_error_errno(errno, "Failed to duplicate fd: %m");
146 goto finish;
147 }
148
149 if (fd >= 3)
150 safe_close(fd);
151 fd = -1;
152
153 if (argc <= optind)
154 (void) execl("/bin/cat", "/bin/cat", NULL);
155 else
156 (void) execvp(argv[optind], argv + optind);
157 r = -errno;
158
159 /* Let's try to restore a working stderr, so we can print the error message */
160 if (saved_stderr >= 0)
161 (void) dup3(saved_stderr, STDERR_FILENO, 0);
162
163 log_error_errno(r, "Failed to execute process: %m");
164
165 finish:
166 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
167 }