aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-04-20 14:08:26 +0200
committerMattias Andrée <maandree@operamail.com>2014-04-20 14:08:26 +0200
commit1c7b03ea54dff23970d39f801963c9be1b3e8b89 (patch)
tree09a4bb08a2d9404e52b9c94e3cd58d682275d7a9 /src/mds.c
parentmisc (diff)
downloadmds-1c7b03ea54dff23970d39f801963c9be1b3e8b89.tar.gz
mds-1c7b03ea54dff23970d39f801963c9be1b3e8b89.tar.bz2
mds-1c7b03ea54dff23970d39f801963c9be1b3e8b89.tar.xz
create pid file and set environment
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/mds.c')
-rw-r--r--src/mds.c55
1 files changed, 45 insertions, 10 deletions
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 <errno.h>
#include <limits.h>
#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+
+
+/**
+ * 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. */