]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - user_list.c
server: Make argument of user_list_init() constant.
[paraslash.git] / user_list.c
index 8c54ab7f78cc150a236d7231283d3b5279d9e3fc..030865fad614a3a9b2b190da0e3cf1d9ceb94a25 100644 (file)
 INITIALIZED_LIST_HEAD(user_list);
 
 /*
- * Fill the list of users known to para_server.
+ * Wrapper for fgets(3).
  *
- * Populates a linked list of all users in \a user_list_file.  Returns on
- * success, calls exit() on errors.
+ * Unlike fgets(3), an integer value is returned. On success, this function
+ * returns 1. On errors, -E_FGETS is returned. A zero return value indicates an
+ * end of file condition.
  */
-static void populate(char *user_list_file)
+static int xfgets(char *line, int size, FILE *f)
+{
+again:
+       if (fgets(line, size, f))
+               return 1;
+       if (feof(f))
+               return 0;
+       if (!ferror(f))
+               return -E_FGETS;
+       if (errno != EINTR) {
+               PARA_ERROR_LOG("%s\n", strerror(errno));
+               return -E_FGETS;
+       }
+       clearerr(f);
+       goto again;
+}
+
+/**
+ * Remove all entries from the user list.
+ *
+ * This is called on shutdown and when the user list is reloaded because the
+ * server received SIGHUP.
+ */
+void user_list_deplete(void)
+{
+       struct user *u, *tmpu;
+
+       list_for_each_entry_safe(u, tmpu, &user_list, node) {
+               list_del(&u->node);
+               free(u->name);
+               free_public_key(u->pubkey);
+               free(u);
+       }
+}
+
+/**
+ * Initialize the list of users allowed to connect to para_server.
+ *
+ * \param user_list_file The file containing access information.
+ *
+ * If this function is called for the second time, the contents of the
+ * previous call are discarded, i.e. the user list is reloaded.
+ *
+ * This function either succeeds or calls exit(3).
+ */
+void user_list_init(const char *user_list_file)
 {
        int ret = -E_USERLIST;
        FILE *file_ptr = fopen(user_list_file, "r");
+       struct user *u;
 
        if (!file_ptr)
                goto err;
+
+       user_list_deplete();
        for (;;) {
                int num;
                char line[255];
                /* keyword, name, key, perms */
                char w[255], n[255], k[255], p[255], tmp[4][255];
-               struct user *u;
                struct asymmetric_key *pubkey;
 
-               ret = para_fgets(line, sizeof(line), file_ptr);
+               ret = xfgets(line, sizeof(line), file_ptr);
                if (ret <= 0)
                        break;
                if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
@@ -93,27 +141,6 @@ err:
        exit(EXIT_FAILURE);
 }
 
-/**
- * Initialize the list of users allowed to connect to para_server.
- *
- * \param user_list_file The file containing access information.
- *
- * If this function is called for the second time, the contents of the
- * previous call are discarded, i.e. the user list is reloaded.
- */
-void user_list_init(char *user_list_file)
-{
-       struct user *u, *tmp;
-
-       list_for_each_entry_safe(u, tmp, &user_list, node) {
-               list_del(&u->node);
-               free(u->name);
-               free_public_key(u->pubkey);
-               free(u);
-       }
-       populate(user_list_file);
-}
-
 /**
  * Lookup a user in the user list.
  *