From 1c7b03ea54dff23970d39f801963c9be1b3e8b89 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 20 Apr 2014 14:08:26 +0200 Subject: create pid file and set environment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/mds.c | 55 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 10 deletions(-) (limited to 'src/mds.c') diff --git a/src/mds.c b/src/mds.c index 90d77ad..eb87415 100644 --- a/src/mds.c +++ b/src/mds.c @@ -25,23 +25,40 @@ #include #include #include +#include +#include + + +/** + * Number of elements in `argv` + */ +static int argc; + +/** + * Command line arguments + */ +static const char** argv; /** * Entry point of the program * - * @param argc Number of elements in `argv` - * @param argv Command line arguments - * @return Non-zero on error + * @param argc_ Number of elements in `argv_` + * @param argv_ Command line arguments + * @return Non-zero on error */ -int main(int argc, const char** argv) +int main(int argc_, const char** argv_) { char pathname[PATH_MAX]; + char piddata[64]; unsigned int display; - FILE *f; int fd; + FILE *f; + + + argc = argc_; + argv = argv_; - (void) argv; /* Sanity check the number of command line arguments. */ if (argc > ARGC_LIMIT) @@ -77,7 +94,6 @@ int main(int argc, const char** argv) /* TODO reuse display index not no longer used */ continue; } - close(fd); } if (display == DISPLAY_MAX) @@ -86,12 +102,31 @@ int main(int argc, const char** argv) "%s: Sorry, too many displays on the system.\n", *argv); return 1; - /* Yes, the directory could have been removed, but probably not. */ + /* Yes, the directory could have been removed, but it probably was not. */ } - /* TODO: Create PID file. */ + /* Create PID file. */ + f = fopen(pathname, "w"); + if (f == NULL) + { + perror(*argv); + return 1; + } + snprintf(piddata, sizeof(piddata) / sizeof(char), "%u\n", getpid()); + if (fwrite(piddata, 1, strlen(piddata), f) < strlen(piddata)) + { + fclose(f); + if (unlink(pathname) < 0) + perror(*argv); + return -1; + } + fflush(f); + fclose(f); - /* TODO: Save MDS_DISPLAY environment variable. */ + /* Save MDS_DISPLAY environment variable. */ + snprintf(pathname, sizeof(pathname) / sizeof(char), /* Excuse the reuse without renaming. */ + "%s=%u", DISPLAY_ENV, display); + putenv(pathname); /* Drop privileges. They most not be propagated non-authorised components. */ /* setgid should not be set, but just to be safe we are restoring both user and group. */ -- cgit v1.2.3-70-g09d2