+static const char *pidfile = "/var/run/rpc.statd.pid";
+
+int pidfd = -1;
+static void create_pidfile(void)
+{
+ FILE *fp;
+
+ unlink(pidfile);
+ fp = fopen(pidfile, "w");
+ if (!fp)
+ die("Opening %s failed: %s\n",
+ pidfile, strerror(errno));
+ fprintf(fp, "%d\n", getpid());
+ pidfd = dup(fileno(fp));
+ if (fclose(fp) < 0)
+ note(N_WARNING, "Flushing pid file failed.\n");
+}
+
+static void truncate_pidfile(void)
+{
+ if (pidfd >= 0)
+ ftruncate(pidfd, 0);
+}
+
+static void drop_privs(void)
+{
+ struct stat st;
+
+ if (stat(SM_DIR, &st) == -1 &&
+ stat(DIR_BASE, &st) == -1) {
+ st.st_uid = 0;
+ st.st_gid = 0;
+ }
+
+ if (st.st_uid == 0) {
+ note(N_WARNING, "statd running as root. chown %s to choose different user\n",
+ SM_DIR);
+ return;
+ }
+ /* better chown the pid file before dropping, as if it
+ * if over nfs we might loose access
+ */
+ if (pidfd >= 0)
+ fchown(pidfd, st.st_uid, st.st_gid);
+
+ setgroups(0, NULL);
+ if (setgid(st.st_gid) == -1
+ || setuid(st.st_uid) == -1) {
+ note(N_ERROR, "Fail to drop privileges");
+ exit(1);
+ }
+}
+
+static void run_sm_notify(int outport)
+{
+ char op[20];
+ char *av[6];
+ int ac = 0;
+
+ av[ac++] = "/usr/sbin/sm-notify";
+ if (run_mode & MODE_NODAEMON)
+ av[ac++] = "-d";
+ if (outport) {
+ sprintf(op, "-p%d", outport);
+ av[ac++] = op;
+ }
+ if (run_mode & STATIC_HOSTNAME) {
+ av[ac++] = "-v";
+ av[ac++] = MY_NAME;
+ }
+ av[ac] = NULL;
+ execv(av[0], av);
+ fprintf(stderr, "%s: failed to run %s\n", name_p, av[0]);
+ exit(2);
+
+}