From 17ade439a85c9cb0074978bb8df873c7ec993b41 Mon Sep 17 00:00:00 2001
From: Simon Ruderich <simon@ruderich.org>
Date: Mon, 17 Jun 2019 20:39:52 +0200
Subject: [PATCH] nss: reduce code duplication in
 _nss_cash_setpwent/_nss_cash_setgrent

---
 nss/gr.c | 19 +++++++++----------
 nss/pw.c | 19 +++++++++----------
 2 files changed, 18 insertions(+), 20 deletions(-)

diff --git a/nss/gr.c b/nss/gr.c
index 79ac540..48e3a1b 100644
--- a/nss/gr.c
+++ b/nss/gr.c
@@ -91,24 +91,23 @@ static struct file static_file = {
 };
 static pthread_mutex_t static_file_lock = PTHREAD_MUTEX_INITIALIZER;
 
-enum nss_status _nss_cash_setgrent(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);
-    // getgrent_r will open the file if necessary when called
     pthread_mutex_unlock(&static_file_lock);
+}
 
+enum nss_status _nss_cash_setgrent(int x) {
+    (void)x;
+
+    // Unmap is necessary to detect changes when the file was replaced on
+    // disk; getgrent_r will open the file if necessary when called
+    internal_unmap_static_file();
     return NSS_STATUS_SUCCESS;
 }
 
 enum nss_status _nss_cash_endgrent(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;
 }
 
diff --git a/nss/pw.c b/nss/pw.c
index e95ec38..c612026 100644
--- a/nss/pw.c
+++ b/nss/pw.c
@@ -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;
 }
 
-- 
2.49.0