From dbf5d92605a9db10e3e3cb154d03a515c5d5c5af Mon Sep 17 00:00:00 2001 From: reinelt Date: Sun, 8 May 2005 04:32:45 +0000 Subject: [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 --- pid.c | 152 ++++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 78 insertions(+), 74 deletions(-) (limited to 'pid.c') diff --git a/pid.c b/pid.c index f3b3f40..6440d48 100644 --- a/pid.c +++ b/pid.c @@ -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); } -- cgit v1.2.3