]> granicus.if.org Git - procps-ng/commitdiff
pmap: Don't reopen stdin for reading file
authorCraig Small <csmall@dropbear.xyz>
Tue, 26 Apr 2022 11:18:03 +0000 (21:18 +1000)
committerCraig Small <csmall@dropbear.xyz>
Tue, 26 Apr 2022 11:18:03 +0000 (21:18 +1000)
pmap uses freopen to read /proc/self/maps. There doesn't
seem to be a good reason to do this and if pmap has its
stdin previously closed then it fails.

Signed-off-by: Craig Small <csmall@dropbear.xyz>
NEWS
pmap.c

diff --git a/NEWS b/NEWS
index 2209e61548d8bcfdd960460741d211a494ccfc0a..f1bdf4a76fc0e55e1e8fca44e98ee619aee62a9a 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,7 @@
 procps-ng-NEXT
   * library
     Re-add elogind support                                 merge #151
+  * pmap: Dont reuse stdin filehandle                      issue #231
   * ps: threads again display when -L is used with -q      issue #234
   * ps: proper aix format string behavior was restored
   * sysctl: print dotted keys again
diff --git a/pmap.c b/pmap.c
index 1e20fe0f5bd275a2af174b0437ad2585bfd8389c..d4fe4c0045d9ab3e691ae92c1c76d0bbf9da4426 100644 (file)
--- a/pmap.c
+++ b/pmap.c
@@ -158,21 +158,25 @@ static void discover_shm_minor(void)
        void *addr;
        int shmid;
        char mapbuf_b[256];
+    FILE *fp;
 
-       if (!freopen("/proc/self/maps", "r", stdin))
+    if ( (fp = fopen("/proc/self/maps", "r")) == NULL)
                return;
 
        /* create */
        shmid = shmget(IPC_PRIVATE, 42, IPC_CREAT | 0666);
        if (shmid == -1)
+    {
                /* failed; oh well */
+        fclose(fp);
                return;
+    }
        /* attach */
        addr = shmat(shmid, NULL, SHM_RDONLY);
        if (addr == (void *)-1)
                goto out_destroy;
 
-       while (fgets(mapbuf_b, sizeof mapbuf_b, stdin)) {
+       while (fgets(mapbuf_b, sizeof mapbuf_b, fp)) {
                char perms[32];
                /* to clean up unprintables */
                char *tmp;
@@ -207,6 +211,7 @@ static void discover_shm_minor(void)
                perror(_("shared memory detach"));
 
 out_destroy:
+    fclose(fp);
        if (shmctl(shmid, IPC_RMID, NULL) && errno != EINVAL)
                perror(_("shared memory remove"));