2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-01-13 23:17:54 +01:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2012 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
2012-04-12 00:20:58 +02:00
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2012-01-13 23:17:54 +01:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2012-04-12 00:20:58 +02:00
|
|
|
Lesser General Public License for more details.
|
2012-01-13 23:17:54 +01:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2012-01-13 23:17:54 +01:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2013-04-14 03:02:03 +02:00
|
|
|
#include <fcntl.h>
|
2015-09-23 03:01:06 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2012-01-13 23:17:54 +01:00
|
|
|
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "sd-journal.h"
|
2012-01-13 23:17:54 +01:00
|
|
|
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "parse-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-10-27 00:40:25 +01:00
|
|
|
#include "syslog-util.h"
|
2012-01-13 23:17:54 +01:00
|
|
|
#include "util.h"
|
|
|
|
|
2016-01-31 23:55:06 +01:00
|
|
|
static const char *arg_identifier = NULL;
|
2012-03-27 00:20:48 +02:00
|
|
|
static int arg_priority = LOG_INFO;
|
2012-01-13 23:17:54 +01:00
|
|
|
static bool arg_level_prefix = true;
|
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
static void help(void) {
|
2012-01-13 23:17:54 +01:00
|
|
|
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
|
|
|
|
"Execute process with stdout/stderr connected to the journal.\n\n"
|
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n"
|
|
|
|
" -t --identifier=STRING Set syslog identifier\n"
|
|
|
|
" -p --priority=PRIORITY Set priority value (0..7)\n"
|
2014-08-02 17:12:21 +02:00
|
|
|
" --level-prefix=BOOL Control whether level prefix shall be parsed\n"
|
|
|
|
, program_invocation_short_name);
|
2012-01-13 23:17:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
ARG_LEVEL_PREFIX
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2012-03-15 22:19:36 +01:00
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
2012-01-13 23:17:54 +01:00
|
|
|
{ "identifier", required_argument, NULL, 't' },
|
|
|
|
{ "priority", required_argument, NULL, 'p' },
|
|
|
|
{ "level-prefix", required_argument, NULL, ARG_LEVEL_PREFIX },
|
2013-11-06 18:28:39 +01:00
|
|
|
{}
|
2012-01-13 23:17:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0)
|
2012-01-13 23:17:54 +01:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
2014-08-02 17:12:21 +02:00
|
|
|
help();
|
|
|
|
return 0;
|
2012-01-13 23:17:54 +01:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
2015-09-23 03:01:06 +02:00
|
|
|
return version();
|
2012-01-13 23:17:54 +01:00
|
|
|
|
|
|
|
case 't':
|
|
|
|
if (isempty(optarg))
|
|
|
|
arg_identifier = NULL;
|
2016-01-31 23:55:06 +01:00
|
|
|
else
|
|
|
|
arg_identifier = optarg;
|
2012-01-13 23:17:54 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
arg_priority = log_level_from_string(optarg);
|
|
|
|
if (arg_priority < 0) {
|
|
|
|
log_error("Failed to parse priority value.");
|
2015-09-23 19:39:30 +02:00
|
|
|
return -EINVAL;
|
2012-01-13 23:17:54 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_LEVEL_PREFIX: {
|
|
|
|
int k;
|
|
|
|
|
|
|
|
k = parse_boolean(optarg);
|
2015-09-23 19:39:51 +02:00
|
|
|
if (k < 0)
|
|
|
|
return log_error_errno(k, "Failed to parse level prefix value.");
|
|
|
|
|
2012-01-13 23:17:54 +01:00
|
|
|
arg_level_prefix = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:28:39 +01:00
|
|
|
case '?':
|
2012-01-13 23:17:54 +01:00
|
|
|
return -EINVAL;
|
2013-11-06 18:28:39 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unhandled option");
|
2012-01-13 23:17:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2015-09-23 19:39:51 +02:00
|
|
|
_cleanup_close_ int fd = -1, saved_stderr = -1;
|
|
|
|
int r;
|
2012-01-13 23:17:54 +01:00
|
|
|
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
fd = sd_journal_stream_fd(arg_identifier, arg_priority, arg_level_prefix);
|
|
|
|
if (fd < 0) {
|
2015-09-23 19:39:51 +02:00
|
|
|
r = log_error_errno(fd, "Failed to create stream fd: %m");
|
2012-01-13 23:17:54 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
|
|
|
|
|
|
|
|
if (dup3(fd, STDOUT_FILENO, 0) < 0 ||
|
|
|
|
dup3(fd, STDERR_FILENO, 0) < 0) {
|
2015-09-08 19:30:45 +02:00
|
|
|
r = log_error_errno(errno, "Failed to duplicate fd: %m");
|
2012-01-13 23:17:54 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2018-02-26 15:41:38 +01:00
|
|
|
fd = safe_close_above_stdio(fd);
|
2012-01-13 23:17:54 +01:00
|
|
|
|
|
|
|
if (argc <= optind)
|
2015-09-23 19:39:51 +02:00
|
|
|
(void) execl("/bin/cat", "/bin/cat", NULL);
|
2012-01-13 23:17:54 +01:00
|
|
|
else
|
2015-09-23 19:39:51 +02:00
|
|
|
(void) execvp(argv[optind], argv + optind);
|
2012-03-15 22:19:36 +01:00
|
|
|
r = -errno;
|
|
|
|
|
2012-01-13 23:17:54 +01:00
|
|
|
/* Let's try to restore a working stderr, so we can print the error message */
|
|
|
|
if (saved_stderr >= 0)
|
2015-09-23 19:39:51 +02:00
|
|
|
(void) dup3(saved_stderr, STDERR_FILENO, 0);
|
2012-01-13 23:17:54 +01:00
|
|
|
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to execute process: %m");
|
2012-01-13 23:17:54 +01:00
|
|
|
|
|
|
|
finish:
|
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|