diff options
author | reinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f> | 2005-05-08 04:32:45 +0000 |
---|---|---|
committer | reinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f> | 2005-05-08 04:32:45 +0000 |
commit | dbf5d92605a9db10e3e3cb154d03a515c5d5c5af (patch) | |
tree | 501d5dda3ce90fe924b893e487d9ef9f0683382b /pid.c | |
parent | a1c471e9fb7cadb68bb182ab2e9715a0aa092386 (diff) | |
download | lcd4linux-dbf5d92605a9db10e3e3cb154d03a515c5d5c5af.tar.gz |
[lcd4linux @ 2005-05-08 04:32:43 by reinelt]
CodingStyle added and applied
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@547 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
Diffstat (limited to 'pid.c')
-rw-r--r-- | pid.c | 152 |
1 files changed, 78 insertions, 74 deletions
@@ -1,4 +1,4 @@ -/* $Id: pid.c,v 1.9 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: pid.c,v 1.10 2005/05/08 04:32:44 reinelt Exp $ * * PID file handling * @@ -22,6 +22,9 @@ * * * $Log: pid.c,v $ + * Revision 1.10 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.9 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -96,91 +99,92 @@ #endif -int pid_init (const char *pidfile) +int pid_init(const char *pidfile) { - char tmpfile[256]; - char buffer[16]; - int fd, len, pid; - - qprintf(tmpfile, sizeof(tmpfile), "%s.%s", pidfile, "XXXXXX"); - - if ((fd=mkstemp(tmpfile))==-1) { - error ("mkstemp(%s) failed: %s", tmpfile, strerror(errno)); - return -1; - } - - if (fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)==-1) { - error ("fchmod(%s) failed: %s", tmpfile, strerror(errno)); - close(fd); - unlink(tmpfile); - return -1; - } - - qprintf(buffer, sizeof(buffer), "%d\n", (int)getpid()); - len = strlen(buffer); - if (write(fd, buffer, len) != len) { - error ("write(%s) failed: %s", tmpfile, strerror(errno)); - close(fd); - unlink(tmpfile); - return -1; - } - close (fd); - - - while (link(tmpfile, pidfile)==-1) { - - if (errno!=EEXIST) { - error ("link(%s, %s) failed: %s", tmpfile, pidfile, strerror(errno)); - unlink(tmpfile); - return -1; - } + char tmpfile[256]; + char buffer[16]; + int fd, len, pid; - if ((fd=open(pidfile, O_RDONLY))==-1) { - if (errno==ENOENT) continue; /* pidfile disappared */ - error ("open(%s) failed: %s", pidfile, strerror(errno)); - unlink (tmpfile); - return -1; - } + qprintf(tmpfile, sizeof(tmpfile), "%s.%s", pidfile, "XXXXXX"); - len=read(fd, buffer, sizeof(buffer)-1); - if (len<0) { - error ("read(%s) failed: %s", pidfile, strerror(errno)); - unlink (tmpfile); - return -1; + if ((fd = mkstemp(tmpfile)) == -1) { + error("mkstemp(%s) failed: %s", tmpfile, strerror(errno)); + return -1; } - - buffer[len]='\0'; - if (sscanf(buffer, "%d", &pid)!=1 || pid==0) { - error ("scan(%s) failed.", pidfile); - unlink (tmpfile); - return -1; + + if (fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) { + error("fchmod(%s) failed: %s", tmpfile, strerror(errno)); + close(fd); + unlink(tmpfile); + return -1; } - if (pid==getpid()) { - error ("%s already locked by us. uh-oh...", pidfile); - unlink(tmpfile); - return 0; + qprintf(buffer, sizeof(buffer), "%d\n", (int) getpid()); + len = strlen(buffer); + if (write(fd, buffer, len) != len) { + error("write(%s) failed: %s", tmpfile, strerror(errno)); + close(fd); + unlink(tmpfile); + return -1; } - - if ((kill(pid, 0)==-1) && errno==ESRCH) { - error ("removing stale PID file %s", pidfile); - if (unlink(pidfile)==-1 && errno!=ENOENT) { - error ("unlink(%s) failed: %s", pidfile, strerror(errno)); + close(fd); + + + while (link(tmpfile, pidfile) == -1) { + + if (errno != EEXIST) { + error("link(%s, %s) failed: %s", tmpfile, pidfile, strerror(errno)); + unlink(tmpfile); + return -1; + } + + if ((fd = open(pidfile, O_RDONLY)) == -1) { + if (errno == ENOENT) + continue; /* pidfile disappared */ + error("open(%s) failed: %s", pidfile, strerror(errno)); + unlink(tmpfile); + return -1; + } + + len = read(fd, buffer, sizeof(buffer) - 1); + if (len < 0) { + error("read(%s) failed: %s", pidfile, strerror(errno)); + unlink(tmpfile); + return -1; + } + + buffer[len] = '\0'; + if (sscanf(buffer, "%d", &pid) != 1 || pid == 0) { + error("scan(%s) failed.", pidfile); + unlink(tmpfile); + return -1; + } + + if (pid == getpid()) { + error("%s already locked by us. uh-oh...", pidfile); + unlink(tmpfile); + return 0; + } + + if ((kill(pid, 0) == -1) && errno == ESRCH) { + error("removing stale PID file %s", pidfile); + if (unlink(pidfile) == -1 && errno != ENOENT) { + error("unlink(%s) failed: %s", pidfile, strerror(errno)); + unlink(tmpfile); + return pid; + } + continue; + } unlink(tmpfile); return pid; - } - continue; } - unlink (tmpfile); - return pid; - } - - unlink (tmpfile); - return 0; + + unlink(tmpfile); + return 0; } -int pid_exit (const char *pidfile) +int pid_exit(const char *pidfile) { - return unlink(pidfile); + return unlink(pidfile); } |