]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/cat.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / journal / cat.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
755a02c6 2/***
755a02c6 3 Copyright 2012 Lennart Poettering
755a02c6
LP
4***/
5
755a02c6 6#include <errno.h>
abad76cc 7#include <fcntl.h>
3f6fd1ba
LP
8#include <getopt.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
755a02c6 12
3f6fd1ba 13#include "sd-journal.h"
755a02c6 14
3ffd4af2 15#include "fd-util.h"
6bedfcbb 16#include "parse-util.h"
07630cea 17#include "string-util.h"
7ccbd1ae 18#include "syslog-util.h"
755a02c6 19#include "util.h"
755a02c6 20
2afcd690 21static const char *arg_identifier = NULL;
d508ac0b 22static int arg_priority = LOG_INFO;
755a02c6
LP
23static bool arg_level_prefix = true;
24
601185b4 25static void help(void) {
755a02c6
LP
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"
601185b4
ZJS
32 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
33 , program_invocation_short_name);
755a02c6
LP
34}
35
36static 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' },
9aac0b2c 45 { "version", no_argument, NULL, ARG_VERSION },
755a02c6
LP
46 { "identifier", required_argument, NULL, 't' },
47 { "priority", required_argument, NULL, 'p' },
48 { "level-prefix", required_argument, NULL, ARG_LEVEL_PREFIX },
eb9da376 49 {}
755a02c6
LP
50 };
51
52 int c;
53
54 assert(argc >= 0);
55 assert(argv);
56
601185b4 57 while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0)
755a02c6
LP
58
59 switch (c) {
60
61 case 'h':
601185b4
ZJS
62 help();
63 return 0;
755a02c6
LP
64
65 case ARG_VERSION:
3f6fd1ba 66 return version();
755a02c6
LP
67
68 case 't':
755a02c6
LP
69 if (isempty(optarg))
70 arg_identifier = NULL;
2afcd690
LP
71 else
72 arg_identifier = optarg;
755a02c6
LP
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.");
e4603df5 79 return -EINVAL;
755a02c6
LP
80 }
81 break;
82
83 case ARG_LEVEL_PREFIX: {
84 int k;
85
86 k = parse_boolean(optarg);
939c173f
LP
87 if (k < 0)
88 return log_error_errno(k, "Failed to parse level prefix value.");
89
755a02c6
LP
90 arg_level_prefix = k;
91 break;
92 }
93
eb9da376 94 case '?':
755a02c6 95 return -EINVAL;
eb9da376
LP
96
97 default:
98 assert_not_reached("Unhandled option");
755a02c6 99 }
755a02c6
LP
100
101 return 1;
102}
103
104int main(int argc, char *argv[]) {
939c173f
LP
105 _cleanup_close_ int fd = -1, saved_stderr = -1;
106 int r;
755a02c6
LP
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) {
939c173f 117 r = log_error_errno(fd, "Failed to create stream fd: %m");
755a02c6
LP
118 goto finish;
119 }
120
121 saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
122
2b33ab09
LP
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");
755a02c6
LP
127 goto finish;
128 }
129
755a02c6 130 if (argc <= optind)
939c173f 131 (void) execl("/bin/cat", "/bin/cat", NULL);
755a02c6 132 else
939c173f 133 (void) execvp(argv[optind], argv + optind);
9aac0b2c
LP
134 r = -errno;
135
755a02c6
LP
136 /* Let's try to restore a working stderr, so we can print the error message */
137 if (saved_stderr >= 0)
939c173f 138 (void) dup3(saved_stderr, STDERR_FILENO, 0);
755a02c6 139
da927ba9 140 log_error_errno(r, "Failed to execute process: %m");
755a02c6
LP
141
142finish:
755a02c6
LP
143 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
144}