-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
+/* SPDX-License-Identifier: LGPL-2.1+ */
/***
- This file is part of systemd.
-
Copyright 2010 Lennart Poettering
-
- systemd is free software; you can redistribute it and/or modify it
- 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
- (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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <stdio.h>
-#include <getopt.h>
-#include <error.h>
#include <errno.h>
-#include <unistd.h>
+#include <getopt.h>
+#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
+#include <unistd.h>
-#include "systemd/sd-daemon.h"
+#include "sd-daemon.h"
+#include "alloc-util.h"
+#include "env-util.h"
+#include "format-util.h"
+#include "log.h"
+#include "parse-util.h"
+#include "string-util.h"
#include "strv.h"
+#include "user-util.h"
#include "util.h"
-#include "log.h"
-#include "sd-readahead.h"
-#include "build.h"
-#include "env-util.h"
static bool arg_ready = false;
static pid_t arg_pid = 0;
static const char *arg_status = NULL;
static bool arg_booted = false;
-static const char *arg_readahead = NULL;
-
-static int help(void) {
+static uid_t arg_uid = UID_INVALID;
+static gid_t arg_gid = GID_INVALID;
+static void help(void) {
printf("%s [OPTIONS...] [VARIABLE=VALUE...]\n\n"
"Notify the init system about service status updates.\n\n"
- " -h --help Show this help\n"
- " --version Show package version\n"
- " --ready Inform the init system about service start-up completion\n"
- " --pid[=PID] Set main pid of daemon\n"
- " --status=TEXT Set status text\n"
- " --booted Returns 0 if the system was booted up with systemd, non-zero otherwise\n"
- " --readahead=ACTION Controls read-ahead operations\n",
+ " -h --help Show this help\n"
+ " --version Show package version\n"
+ " --ready Inform the init system about service start-up completion\n"
+ " --pid[=PID] Set main PID of daemon\n"
+ " --uid=USER Set user to send from\n"
+ " --status=TEXT Set status text\n"
+ " --booted Check if the system was booted up with systemd\n",
program_invocation_short_name);
-
- return 0;
}
static int parse_argv(int argc, char *argv[]) {
ARG_PID,
ARG_STATUS,
ARG_BOOTED,
- ARG_READAHEAD
+ ARG_UID,
};
static const struct option options[] = {
{ "pid", optional_argument, NULL, ARG_PID },
{ "status", required_argument, NULL, ARG_STATUS },
{ "booted", no_argument, NULL, ARG_BOOTED },
- { "readahead", required_argument, NULL, ARG_READAHEAD },
+ { "uid", required_argument, NULL, ARG_UID },
{}
};
- int c;
+ int c, r;
assert(argc >= 0);
assert(argv);
switch (c) {
case 'h':
- return help();
+ help();
+ return 0;
case ARG_VERSION:
- puts(PACKAGE_STRING);
- puts(SYSTEMD_FEATURES);
- return 0;
+ return version();
case ARG_READY:
arg_ready = true;
arg_booted = true;
break;
- case ARG_READAHEAD:
- arg_readahead = optarg;
+ 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;
+ }
case '?':
return -EINVAL;
!arg_ready &&
!arg_status &&
!arg_pid &&
- !arg_booted &&
- !arg_readahead) {
+ !arg_booted) {
help();
return -EINVAL;
}
if (arg_booted)
return sd_booted() <= 0;
- if (arg_readahead) {
- r = sd_readahead(arg_readahead);
- if (r < 0) {
- log_error("Failed to issue read-ahead control command: %s", strerror(-r));
- goto finish;
- }
- }
-
if (arg_ready)
our_env[i++] = (char*) "READY=1";
goto finish;
}
- if (strv_length(final_env) <= 0) {
+ if (strv_isempty(final_env)) {
r = 0;
goto finish;
}
goto finish;
}
- r = sd_pid_notify(arg_pid, false, n);
+ /* 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;
+ }
+
+ r = sd_pid_notify(arg_pid ? arg_pid : getppid(), false, n);
if (r < 0) {
- log_error("Failed to notify init system: %s", strerror(-r));
+ log_error_errno(r, "Failed to notify init system: %m");
goto finish;
+ } else if (r == 0) {
+ log_error("No status data could be sent: $NOTIFY_SOCKET was not set");
+ r = -EOPNOTSUPP;
}
- if (r == 0)
- r = -ENOTSUP;
-
finish:
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}