chiark / gitweb /
compute Sentence_Break as well
[disorder] / lib / user.c
index 7cbca987f0ee8fb6c223e8a5fb431261d7bd64bb..ec01a339bc6a7259294b07d9b98d8d51ce426672 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder
- * Copyright (C) 2005 Richard Kettlewell
+ * Copyright (C) 2005, 2007 Richard Kettlewell
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  * USA
  */
+/** @file lib/user.c
+ * @brief Jukebox user management
+ */
 
 #include <config.h>
 #include "types.h"
 
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <errno.h>
 #include <pwd.h>
 #include <grp.h>
 #include "user.h"
 #include "log.h"
 #include "configuration.h"
+#include "mem.h"
 
+/** @brief Become the jukebox user
+ *
+ * If a jukebox user is configured then becomes that user.
+ */
 void become_mortal(void) {
   struct passwd *pw;
   
@@ -54,6 +63,41 @@ void become_mortal(void) {
   }
 }
 
+/** @brief Create the jukebox state directory
+ *
+ * If the home directory does not exist then creates it and assigns
+ * it suitable permissions.
+ */
+void make_home(void) {
+  struct stat sb;
+  struct passwd *pw;
+  char *home, *p;
+  
+  if(stat(config->home, &sb) < 0) {
+    /* create parent directories */
+    home = xstrdup(config->home);
+    p = home;
+    while(*p) {
+      if(*p == '/' && p > home) {
+        *p = 0;
+        mkdir(home, 0755);
+        *p = '/';
+      }
+      ++p;
+    }
+    /* create the directory itself */
+    if(mkdir(config->home, 02755) < 0)
+      fatal(errno, "error creating %s", config->home);
+    /* make sure it has the right ownership */
+    if(config->user) {
+      if(!(pw = getpwnam(config->user)))
+        fatal(0, "cannot find user %s", config->user);
+      if(chown(config->home, pw->pw_uid, pw->pw_gid) < 0)
+        fatal(errno, "error chowning %s", config->home);
+    }
+  }
+}
+
 /*
 Local Variables:
 c-basic-offset:2
@@ -62,4 +106,3 @@ fill-column:79
 indent-tabs-mode:nil
 End:
 */
-/* arch-tag:GTEl+AUapne19BB73yRZdw */