]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - nss/pw.c
nss: remove pointer indirection in search_key's id member
[nsscash/nsscash.git] / nss / pw.c
index c7e056666216c1cf7e3143dd5626583d1b22298d..78a252f6db5a41edc02aead332670191eaa31ec7 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;
 }
 
@@ -151,7 +150,7 @@ static enum nss_status internal_getpw(struct search_key *key, struct passwd *res
         return NSS_STATUS_NOTFOUND;
     }
 
-    const char *e = h->data + h->off_data + *off;
+    const char *e = key->data + *off;
     if (!entry_to_passwd((struct passwd_entry *)e, result, buffer, buflen)) {
         unmap_file(&f);
         errno = ERANGE;
@@ -164,9 +163,8 @@ static enum nss_status internal_getpw(struct search_key *key, struct passwd *res
 }
 
 enum nss_status _nss_cash_getpwuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
-    uint64_t id = (uint64_t)uid;
     struct search_key key = {
-        .id = &id,
+        .id = (uint64_t)uid,
         .offset = offsetof(struct passwd_entry, uid),
     };
     return internal_getpw(&key, result, buffer, buflen, errnop);