+ fprintf(stderr," -h, -?, --help Print this help screen.\n");
+ fprintf(stderr," -F, --foreground Foreground (no-daemon mode)\n");
+ fprintf(stderr," -d, --no-syslog Verbose logging to stderr. Foreground mode only.\n");
+ fprintf(stderr," -p, --port Port to listen on\n");
+ fprintf(stderr," -o, --outgoing-port Port for outgoing connections\n");
+ fprintf(stderr," -V, -v, --version Display version information and exit.\n");
+ fprintf(stderr," -n, --name Specify a local hostname.\n");
+ fprintf(stderr," -P State directory path.\n");
+ fprintf(stderr," -N Run in notify only mode.\n");
+}
+
+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;
+
+ 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);
+ }