+
+/*
+ * Record pid in /var/run/sm-notify.pid
+ * This file should remain until a reboot, even if the
+ * program exits.
+ * If file already exists, fail.
+ */
+static int record_pid(void)
+{
+ char pid[20];
+ int fd;
+
+ snprintf(pid, 20, "%d\n", getpid());
+ fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
+ if (fd < 0)
+ return 0;
+ write(fd, pid, strlen(pid));
+ close(fd);
+ return 1;
+}
+
+/* Drop privileges to match owner of state-directory
+ * (in case a reply triggers some unknown bug).
+ */
+static void drop_privs(void)
+{
+ struct stat st;
+
+ if (stat(_SM_DIR_PATH, &st) == -1 &&
+ stat(_SM_BASE_PATH, &st) == -1) {
+ st.st_uid = 0;
+ st.st_gid = 0;
+ }
+
+ if (st.st_uid == 0) {
+ nsm_log(LOG_WARNING,
+ "sm-notify running as root. chown %s to choose different user\n",
+ _SM_DIR_PATH);
+ return;
+ }
+
+ setgroups(0, NULL);
+ if (setgid(st.st_gid) == -1
+ || setuid(st.st_uid) == -1) {
+ nsm_log(LOG_ERR, "Fail to drop privileges");
+ exit(1);
+ }
+}
+
+static void set_kernel_nsm_state(int state)
+{
+ int fd;
+
+ fd = open("/proc/sys/fs/nfs/nsm_local_state",O_WRONLY);
+ if (fd >= 0) {
+ char buf[20];
+ snprintf(buf, sizeof(buf), "%d", state);
+ write(fd, buf, strlen(buf));
+ close(fd);
+ }
+}