return 0;
}
+/*!
+ * Check lockfile
+ */
+int check_lockfile(const char *program, const char *pidfile)
+{
+ char buf[10];
+ FILE *pf;
+ pid_t pid;
+
+ /* check for pid. this can get fooled by stale pid's. */
+ if ((pf = fopen(pidfile, "r"))) {
+ if (fgets(buf, sizeof(buf), pf) && !kill(pid = atol(buf), 0)) {
+ fprintf(stderr, "%s is already running (pid = %d), or the lock file is stale.\n",
+ program, pid);
+ fclose(pf);
+ return -1;
+ }
+ fclose(pf);
+ }
+ return 0;
+}
+
+/*!
+ * Check and create lockfile
+ */
+int create_lockfile(const char *program, const char *pidfile)
+{
+ FILE *pf;
+ int mask;
+
+ if (check_lockfile(program, pidfile) != 0)
+ return -1;
+
+ /* Write PID to pidfile */
+ mask = umask(022);
+ if ((pf = fopen(pidfile, "w")) == NULL) {
+ fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program,
+ pidfile);
+ return -1;
+ }
+ umask(mask);
+ fprintf(pf, "%d\n", getpid());
+ fclose(pf);
+
+ return 0;
+}