]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - nss/pw.c
nss: reduce code duplication in _nss_cash_setpwent/_nss_cash_setgrent
[nsscash/nsscash.git] / nss / pw.c
index c772bb10133e2d7c29feb4565f6327739cb2497e..c612026aa132a96cd7a6a303016e7bc7a27d5ad3 100644 (file)
--- a/nss/pw.c
+++ b/nss/pw.c
@@ -36,18 +36,18 @@ struct passwd_entry {
     uint64_t uid;
     uint64_t gid;
 
-    //       off_name = 0
+    //       off_name = 0, not stored on disk
     uint16_t off_passwd;
     uint16_t off_gecos;
     uint16_t off_dir;
     uint16_t off_shell;
 
-    uint16_t data_size;
     /*
      * Data contains all strings (name, passwd, gecos, dir, shell)
      * concatenated, with their trailing NUL. The off_* variables point to
      * beginning of each string.
      */
+    uint16_t data_size; // size of data in bytes
     const char data[];
 } __attribute__((packed));
 
@@ -74,24 +74,23 @@ static struct file static_file = {
 };
 static pthread_mutex_t static_file_lock = PTHREAD_MUTEX_INITIALIZER;
 
-enum nss_status _nss_cash_setpwent(int x) {
-    (void)x;
-
+static void internal_unmap_static_file(void) {
     pthread_mutex_lock(&static_file_lock);
-    // Unmap is necessary to detect changes when the file was replaced on
-    // disk
     unmap_file(&static_file);
-    // getpwent_r will open the file if necessary when called
     pthread_mutex_unlock(&static_file_lock);
+}
 
+enum nss_status _nss_cash_setpwent(int x) {
+    (void)x;
+
+    // Unmap is necessary to detect changes when the file was replaced on
+    // disk; getpwent_r will open the file if necessary when called
+    internal_unmap_static_file();
     return NSS_STATUS_SUCCESS;
 }
 
 enum nss_status _nss_cash_endpwent(void) {
-    pthread_mutex_lock(&static_file_lock);
-    unmap_file(&static_file);
-    pthread_mutex_unlock(&static_file_lock);
-
+    internal_unmap_static_file();
     return NSS_STATUS_SUCCESS;
 }