2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2010-06-17 22:50:06 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-09-19 01:56:12 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
2010-06-17 22:50:06 +02:00
|
|
|
#include <stdlib.h>
|
2015-09-19 01:56:12 +02:00
|
|
|
#include <unistd.h>
|
2010-06-17 22:50:06 +02:00
|
|
|
|
2015-09-19 01:56:12 +02:00
|
|
|
#include "sd-daemon.h"
|
2012-01-05 16:01:58 +01:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2013-02-11 03:46:08 +01:00
|
|
|
#include "env-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2015-09-19 01:56:12 +02:00
|
|
|
#include "log.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-09-19 01:56:12 +02:00
|
|
|
#include "strv.h"
|
2018-01-05 13:26:38 +01:00
|
|
|
#include "user-util.h"
|
2015-09-19 01:56:12 +02:00
|
|
|
#include "util.h"
|
2010-06-17 22:50:06 +02:00
|
|
|
|
|
|
|
static bool arg_ready = false;
|
|
|
|
static pid_t arg_pid = 0;
|
|
|
|
static const char *arg_status = NULL;
|
2010-06-21 19:20:21 +02:00
|
|
|
static bool arg_booted = false;
|
2018-01-05 13:26:38 +01:00
|
|
|
static uid_t arg_uid = UID_INVALID;
|
|
|
|
static gid_t arg_gid = GID_INVALID;
|
2010-06-17 22:50:06 +02:00
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
static void help(void) {
|
2010-06-24 00:08:42 +02:00
|
|
|
printf("%s [OPTIONS...] [VARIABLE=VALUE...]\n\n"
|
2010-06-17 22:50:06 +02:00
|
|
|
"Notify the init system about service status updates.\n\n"
|
2015-01-28 03:22:57 +01:00
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n"
|
|
|
|
" --ready Inform the init system about service start-up completion\n"
|
2018-01-05 13:26:38 +01:00
|
|
|
" --pid[=PID] Set main PID of daemon\n"
|
|
|
|
" --uid=USER Set user to send from\n"
|
2015-01-28 03:22:57 +01:00
|
|
|
" --status=TEXT Set status text\n"
|
|
|
|
" --booted Check if the system was booted up with systemd\n",
|
2010-06-17 22:50:06 +02:00
|
|
|
program_invocation_short_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_READY = 0x100,
|
2012-03-15 22:19:36 +01:00
|
|
|
ARG_VERSION,
|
2010-06-17 22:50:06 +02:00
|
|
|
ARG_PID,
|
2010-06-21 19:20:21 +02:00
|
|
|
ARG_STATUS,
|
2010-09-26 15:50:14 +02:00
|
|
|
ARG_BOOTED,
|
2018-01-05 13:26:38 +01:00
|
|
|
ARG_UID,
|
2010-06-17 22:50:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
2010-09-26 15:50:14 +02:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2012-03-15 22:19:36 +01:00
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
2010-09-26 15:50:14 +02:00
|
|
|
{ "ready", no_argument, NULL, ARG_READY },
|
|
|
|
{ "pid", optional_argument, NULL, ARG_PID },
|
|
|
|
{ "status", required_argument, NULL, ARG_STATUS },
|
|
|
|
{ "booted", no_argument, NULL, ARG_BOOTED },
|
2018-01-05 13:26:38 +01:00
|
|
|
{ "uid", required_argument, NULL, ARG_UID },
|
2013-11-06 18:28:39 +01:00
|
|
|
{}
|
2010-06-17 22:50:06 +02:00
|
|
|
};
|
|
|
|
|
2018-01-05 13:26:38 +01:00
|
|
|
int c, r;
|
2010-06-17 22:50:06 +02:00
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2014-08-12 01:41:42 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
|
2010-06-17 22:50:06 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
2014-08-02 17:12:21 +02:00
|
|
|
help();
|
|
|
|
return 0;
|
2010-06-17 22:50:06 +02:00
|
|
|
|
2012-03-15 22:19:36 +01:00
|
|
|
case ARG_VERSION:
|
2015-09-23 03:01:06 +02:00
|
|
|
return version();
|
2012-03-15 22:19:36 +01:00
|
|
|
|
2010-06-17 22:50:06 +02:00
|
|
|
case ARG_READY:
|
|
|
|
arg_ready = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_PID:
|
|
|
|
|
|
|
|
if (optarg) {
|
|
|
|
if (parse_pid(optarg, &arg_pid) < 0) {
|
|
|
|
log_error("Failed to parse PID %s.", optarg);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
arg_pid = getppid();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_STATUS:
|
|
|
|
arg_status = optarg;
|
|
|
|
break;
|
|
|
|
|
2010-06-21 19:20:21 +02:00
|
|
|
case ARG_BOOTED:
|
|
|
|
arg_booted = true;
|
|
|
|
break;
|
|
|
|
|
2018-01-05 13:26:38 +01:00
|
|
|
case ARG_UID: {
|
|
|
|
const char *u = optarg;
|
|
|
|
|
|
|
|
r = get_user_creds(&u, &arg_uid, &arg_gid, NULL, NULL);
|
|
|
|
if (r == -ESRCH) /* If the user doesn't exist, then accept it anyway as numeric */
|
|
|
|
r = parse_uid(u, &arg_uid);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Can't resolve user %s: %m", optarg);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:50:06 +02:00
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
2013-11-06 18:28:39 +01:00
|
|
|
assert_not_reached("Unhandled option");
|
2010-06-17 22:50:06 +02:00
|
|
|
}
|
2014-08-12 01:41:42 +02:00
|
|
|
}
|
2010-06-17 22:50:06 +02:00
|
|
|
|
2010-06-24 04:55:57 +02:00
|
|
|
if (optind >= argc &&
|
|
|
|
!arg_ready &&
|
|
|
|
!arg_status &&
|
|
|
|
!arg_pid &&
|
2014-08-26 21:17:22 +02:00
|
|
|
!arg_booted) {
|
2010-06-24 04:55:57 +02:00
|
|
|
help();
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:50:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2014-06-05 17:05:18 +02:00
|
|
|
_cleanup_free_ char *status = NULL, *cpid = NULL, *n = NULL;
|
|
|
|
_cleanup_strv_free_ char **final_env = NULL;
|
|
|
|
char* our_env[4];
|
2010-06-17 22:50:06 +02:00
|
|
|
unsigned i = 0;
|
2014-06-05 17:05:18 +02:00
|
|
|
int r;
|
2010-06-17 22:50:06 +02:00
|
|
|
|
|
|
|
log_parse_environment();
|
2010-08-16 22:39:02 +02:00
|
|
|
log_open();
|
2010-06-17 22:50:06 +02:00
|
|
|
|
2013-07-01 00:03:57 +02:00
|
|
|
r = parse_argv(argc, argv);
|
2014-06-05 17:05:18 +02:00
|
|
|
if (r <= 0)
|
2010-06-17 22:50:06 +02:00
|
|
|
goto finish;
|
|
|
|
|
2010-06-21 19:20:21 +02:00
|
|
|
if (arg_booted)
|
|
|
|
return sd_booted() <= 0;
|
|
|
|
|
2010-06-17 22:50:06 +02:00
|
|
|
if (arg_ready)
|
|
|
|
our_env[i++] = (char*) "READY=1";
|
|
|
|
|
|
|
|
if (arg_status) {
|
2014-06-05 17:05:18 +02:00
|
|
|
status = strappend("STATUS=", arg_status);
|
|
|
|
if (!status) {
|
|
|
|
r = log_oom();
|
2010-06-17 22:50:06 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
our_env[i++] = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_pid > 0) {
|
2014-04-25 13:45:15 +02:00
|
|
|
if (asprintf(&cpid, "MAINPID="PID_FMT, arg_pid) < 0) {
|
2014-06-05 17:05:18 +02:00
|
|
|
r = log_oom();
|
2010-06-17 22:50:06 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
our_env[i++] = cpid;
|
|
|
|
}
|
|
|
|
|
|
|
|
our_env[i++] = NULL;
|
|
|
|
|
2014-06-05 17:05:18 +02:00
|
|
|
final_env = strv_env_merge(2, our_env, argv + optind);
|
|
|
|
if (!final_env) {
|
|
|
|
r = log_oom();
|
2010-06-17 22:50:06 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:36:35 +01:00
|
|
|
if (strv_isempty(final_env)) {
|
2014-06-05 17:05:18 +02:00
|
|
|
r = 0;
|
2010-06-17 22:50:06 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2014-06-05 17:05:18 +02:00
|
|
|
n = strv_join(final_env, "\n");
|
|
|
|
if (!n) {
|
|
|
|
r = log_oom();
|
2010-06-17 22:50:06 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2018-01-05 13:26:38 +01:00
|
|
|
/* If this is requested change to the requested UID/GID. Note thta we only change the real UID here, and leave
|
|
|
|
the effective UID in effect (which is 0 for this to work). That's because we want the privileges to fake the
|
|
|
|
ucred data, and sd_pid_notify() uses the real UID for filling in ucred. */
|
|
|
|
|
|
|
|
if (arg_gid != GID_INVALID)
|
|
|
|
if (setregid(arg_gid, (gid_t) -1) < 0) {
|
|
|
|
r = log_error_errno(errno, "Failed to change GID: %m");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_uid != UID_INVALID)
|
|
|
|
if (setreuid(arg_uid, (uid_t) -1) < 0) {
|
|
|
|
r = log_error_errno(errno, "Failed to change UID: %m");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2015-09-19 21:57:51 +02:00
|
|
|
r = sd_pid_notify(arg_pid ? arg_pid : getppid(), false, n);
|
2014-06-05 17:05:18 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to notify init system: %m");
|
2010-06-17 22:50:06 +02:00
|
|
|
goto finish;
|
2015-09-09 13:51:58 +02:00
|
|
|
} else if (r == 0) {
|
|
|
|
log_error("No status data could be sent: $NOTIFY_SOCKET was not set");
|
2015-03-13 14:08:00 +01:00
|
|
|
r = -EOPNOTSUPP;
|
2015-09-09 13:51:58 +02:00
|
|
|
}
|
2010-06-17 22:50:06 +02:00
|
|
|
|
|
|
|
finish:
|
2014-06-05 17:05:18 +02:00
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
2010-06-17 22:50:06 +02:00
|
|
|
}
|