X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=blobdiff_plain;f=libatalk%2Futil%2Fserver_lock.c;h=0251eadaeba94652c2b45474881f648ea1c5dde2;hp=90dc11445eabc56cba7121d7888e15186ab3a30a;hb=2fdd522410f80afcd055d7333f491ee6c0b4b9fa;hpb=dd7973d74cca6c48567a576ab2af4080aba64320 diff --git a/libatalk/util/server_lock.c b/libatalk/util/server_lock.c index 90dc1144..0251eada 100644 --- a/libatalk/util/server_lock.c +++ b/libatalk/util/server_lock.c @@ -99,3 +99,51 @@ pid_t server_lock(char *program, char *pidfile, int debug) 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) +{ + char buf[10]; + FILE *pf; + pid_t pid; + 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; +}