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