aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-04-20 21:39:05 +0200
committerMattias Andrée <maandree@operamail.com>2014-04-20 21:39:05 +0200
commitd455e2a1292ee2c3577c7268796c7f596e2cc395 (patch)
tree552fe7160c4e50fae0a693c0fc2815158a4e5de5 /src/mds.c
parentcreate pid file and set environment (diff)
downloadmds-d455e2a1292ee2c3577c7268796c7f596e2cc395.tar.gz
mds-d455e2a1292ee2c3577c7268796c7f596e2cc395.tar.bz2
mds-d455e2a1292ee2c3577c7268796c7f596e2cc395.tar.xz
m + reuse display indices
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r--src/mds.c51
1 files changed, 50 insertions, 1 deletions
diff --git a/src/mds.c b/src/mds.c
index eb87415..95cd6da 100644
--- a/src/mds.c
+++ b/src/mds.c
@@ -91,10 +91,59 @@ int main(int argc_, const char** argv_)
fd = open(pathname, O_CREAT | O_EXCL);
if (fd == -1)
{
- /* TODO reuse display index not no longer used */
+ /* Reuse display index not no longer used. */
+ size_t read_len;
+ f = fopen(pathname, "r");
+ if (f == NULL) /* Race, or error? */
+ {
+ perror(*argv);
+ continue;
+ }
+ read_len = read(piddata, 1, sizeof(piddata) / sizeof(char), f);
+ if (ferror(f)) /* Failed to read. */
+ perror(*argv);
+ else if (feof(f) == 0) /* Did not read everything. */
+ fprintf(stderr,
+ "%s: the content of a PID file is longer than expected.\n",
+ *argv);
+ else
+ {
+ pid_t pid = 0;
+ size_t i, n = read_len - 1;
+ for (i = 0; i < n; i++)
+ {
+ char c = piddata[i];
+ if (('0' <= c) && (c <= '9'))
+ pid = pid * 10 + (c & 15);
+ else
+ {
+ fprintf(stderr,
+ "%s: the content of a PID file is invalid.\n",
+ *argv);
+ goto bad;
+ }
+ }
+ if (piddata[n] != '\n')
+ {
+ fprintf(stderr,
+ "%s: the content of a PID file is invalid.\n",
+ *argv);
+ goto bad;
+ }
+ if (kill(pid, 0) < 0) /* Check if the PID is still allocated to any process. */
+ if (errno == ESRCH) /* PID is not used. */
+ {
+ fclose(f);
+ close(fd);
+ break;
+ }
+ }
+ bad:
+ fclose(f);
continue;
}
close(fd);
+ break;
}
if (display == DISPLAY_MAX)
{