| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <sys/stat.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "sd-journal.h" |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "build.h" |
| 12 |
#include "env-util.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "format-table.h" |
| 15 |
#include "format-util.h" |
| 16 |
#include "log.h" |
| 17 |
#include "main-func.h" |
| 18 |
#include "options.h" |
| 19 |
#include "parse-argument.h" |
| 20 |
#include "pretty-print.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include "strv.h" |
| 23 |
#include "syslog-util.h" |
| 24 |
|
| 25 |
static const char *arg_identifier = NULL; |
| 26 |
static const char *arg_namespace = NULL; |
| 27 |
static int arg_priority = LOG_INFO; |
| 28 |
static int arg_stderr_priority = -1; |
| 29 |
static bool arg_level_prefix = true; |
| 30 |
|
| 31 |
static int help(void) { |
| 32 |
_cleanup_free_ char *link = NULL; |
| 33 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 34 |
int r; |
| 35 |
|
| 36 |
r = terminal_urlify_man("systemd-cat", "1", &link); |
| 37 |
if (r < 0) |
| 38 |
return log_oom(); |
| 39 |
|
| 40 |
r = option_parser_get_help_table(&options); |
| 41 |
if (r < 0) |
| 42 |
return r; |
| 43 |
|
| 44 |
printf("%s [OPTIONS...] COMMAND ...\n\n" |
| 45 |
"%sExecute process with stdout/stderr connected to the journal.%s\n\n", |
| 46 |
program_invocation_short_name, |
| 47 |
ansi_highlight(), |
| 48 |
ansi_normal()); |
| 49 |
|
| 50 |
r = table_print_or_warn(options); |
| 51 |
if (r < 0) |
| 52 |
return r; |
| 53 |
|
| 54 |
printf("\nSee the %s for details.\n", link); |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 59 |
assert(argc >= 0); |
| 60 |
assert(argv); |
| 61 |
|
| 62 |
OptionParser opts = { argc, argv, OPTION_PARSER_STOP_AT_FIRST_NONOPTION }; |
| 63 |
int r; |
| 64 |
|
| 65 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 66 |
switch (c) { |
| 67 |
|
| 68 |
OPTION_COMMON_HELP: |
| 69 |
return help(); |
| 70 |
|
| 71 |
OPTION_COMMON_VERSION: |
| 72 |
return version(); |
| 73 |
|
| 74 |
OPTION('t', "identifier", "STRING", "Set syslog identifier"): |
| 75 |
arg_identifier = empty_to_null(opts.arg); |
| 76 |
break; |
| 77 |
|
| 78 |
OPTION('p', "priority", "PRIORITY", "Set priority value (0..7)"): |
| 79 |
arg_priority = log_level_from_string(opts.arg); |
| 80 |
if (arg_priority < 0) |
| 81 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 82 |
"Failed to parse priority value."); |
| 83 |
break; |
| 84 |
|
| 85 |
OPTION_LONG("stderr-priority", "PRIORITY", |
| 86 |
"Set priority value (0..7) used for stderr"): |
| 87 |
arg_stderr_priority = log_level_from_string(opts.arg); |
| 88 |
if (arg_stderr_priority < 0) |
| 89 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 90 |
"Failed to parse stderr priority value."); |
| 91 |
break; |
| 92 |
|
| 93 |
OPTION_LONG("level-prefix", "BOOL", |
| 94 |
"Control whether level prefix shall be parsed"): |
| 95 |
r = parse_boolean_argument("--level-prefix=", opts.arg, &arg_level_prefix); |
| 96 |
if (r < 0) |
| 97 |
return r; |
| 98 |
break; |
| 99 |
|
| 100 |
OPTION_LONG("namespace", "NAMESPACE", |
| 101 |
"Connect to specified journal namespace"): |
| 102 |
arg_namespace = empty_to_null(opts.arg); |
| 103 |
break; |
| 104 |
} |
| 105 |
|
| 106 |
*ret_args = option_parser_get_args(&opts); |
| 107 |
return 1; |
| 108 |
} |
| 109 |
|
| 110 |
static int run(int argc, char *argv[]) { |
| 111 |
_cleanup_close_ int outfd = -EBADF, errfd = -EBADF, saved_stderr = -EBADF; |
| 112 |
int r; |
| 113 |
|
| 114 |
log_setup(); |
| 115 |
|
| 116 |
char **args = NULL; |
| 117 |
r = parse_argv(argc, argv, &args); |
| 118 |
if (r <= 0) |
| 119 |
return r; |
| 120 |
|
| 121 |
outfd = sd_journal_stream_fd_with_namespace(arg_namespace, arg_identifier, arg_priority, arg_level_prefix); |
| 122 |
if (outfd < 0) |
| 123 |
return log_error_errno(outfd, "Failed to create stream fd: %m"); |
| 124 |
|
| 125 |
if (arg_stderr_priority >= 0 && arg_stderr_priority != arg_priority) { |
| 126 |
errfd = sd_journal_stream_fd_with_namespace(arg_namespace, arg_identifier, arg_stderr_priority, arg_level_prefix); |
| 127 |
if (errfd < 0) |
| 128 |
return log_error_errno(errfd, "Failed to create stream fd: %m"); |
| 129 |
} |
| 130 |
|
| 131 |
saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3); |
| 132 |
|
| 133 |
r = rearrange_stdio(STDIN_FILENO, outfd, errfd < 0 ? outfd : errfd); /* Invalidates fd on success + error! */ |
| 134 |
TAKE_FD(outfd); |
| 135 |
TAKE_FD(errfd); |
| 136 |
if (r < 0) |
| 137 |
return log_error_errno(r, "Failed to rearrange stdout/stderr: %m"); |
| 138 |
|
| 139 |
if (strv_isempty(args)) |
| 140 |
(void) execlp("cat", "cat", NULL); |
| 141 |
else { |
| 142 |
struct stat st; |
| 143 |
|
| 144 |
if (fstat(STDERR_FILENO, &st) < 0) |
| 145 |
return log_error_errno(errno, |
| 146 |
"Failed to fstat(%s): %m", |
| 147 |
FORMAT_PROC_FD_PATH(STDERR_FILENO)); |
| 148 |
|
| 149 |
r = setenvf("JOURNAL_STREAM", /* overwrite= */ true, DEV_FMT ":" INO_FMT, (dev_t) st.st_dev, st.st_ino); |
| 150 |
if (r < 0) |
| 151 |
return log_error_errno(r, "Failed to set environment variable JOURNAL_STREAM: %m"); |
| 152 |
|
| 153 |
(void) execvp(args[0], args); |
| 154 |
} |
| 155 |
r = -errno; |
| 156 |
|
| 157 |
/* Let's try to restore a working stderr, so we can print the error message */ |
| 158 |
if (saved_stderr >= 0) |
| 159 |
(void) dup3(saved_stderr, STDERR_FILENO, 0); |
| 160 |
|
| 161 |
return log_error_errno(r, "Failed to execute process: %m"); |
| 162 |
} |
| 163 |
|
| 164 |
DEFINE_MAIN_FUNCTION(run); |