root/maint/gnulib/lib/readutmp.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. extract_trimmed_name
  2. desirable_utmp_entry
  3. read_utmp
  4. read_utmp

   1 /* GNU's read utmp module.
   2 
   3    Copyright (C) 1992-2001, 2003-2006, 2009-2021 Free Software Foundation, Inc.
   4 
   5    This program is free software: you can redistribute it and/or modify
   6    it under the terms of the GNU General Public License as published by
   7    the Free Software Foundation; either version 3 of the License, or
   8    (at your option) any later version.
   9 
  10    This program is distributed in the hope that it will be useful,
  11    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13    GNU General Public License for more details.
  14 
  15    You should have received a copy of the GNU General Public License
  16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  17 
  18 /* Written by jla; revised by djm */
  19 
  20 #include <config.h>
  21 
  22 #include "readutmp.h"
  23 
  24 #include <errno.h>
  25 #include <stdio.h>
  26 
  27 #include <sys/types.h>
  28 #include <sys/stat.h>
  29 #include <signal.h>
  30 #include <stdbool.h>
  31 #include <string.h>
  32 #include <stdlib.h>
  33 #include <stdint.h>
  34 
  35 #include "xalloc.h"
  36 
  37 /* Each of the FILE streams in this file is only used in a single thread.  */
  38 #include "unlocked-io.h"
  39 
  40 #if 8 <= __GNUC__
  41 # pragma GCC diagnostic ignored "-Wsizeof-pointer-memaccess"
  42 #endif
  43 
  44 /* Copy UT->ut_name into storage obtained from malloc.  Then remove any
  45    trailing spaces from the copy, NUL terminate it, and return the copy.  */
  46 
  47 char *
  48 extract_trimmed_name (const STRUCT_UTMP *ut)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 {
  50   char *p, *trimmed_name;
  51 
  52   trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1);
  53   strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));
  54   /* Append a trailing NUL.  Some systems pad names shorter than the
  55      maximum with spaces, others pad with NULs.  Remove any trailing
  56      spaces.  */
  57   trimmed_name[sizeof (UT_USER (ut))] = '\0';
  58   for (p = trimmed_name + strlen (trimmed_name);
  59        trimmed_name < p && p[-1] == ' ';
  60        *--p = '\0')
  61     continue;
  62   return trimmed_name;
  63 }
  64 
  65 /* Is the utmp entry U desired by the user who asked for OPTIONS?  */
  66 
  67 static bool
  68 desirable_utmp_entry (STRUCT_UTMP const *u, int options)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70   bool user_proc = IS_USER_PROCESS (u);
  71   if ((options & READ_UTMP_USER_PROCESS) && !user_proc)
  72     return false;
  73   if ((options & READ_UTMP_CHECK_PIDS)
  74       && user_proc
  75       && 0 < UT_PID (u)
  76       && (kill (UT_PID (u), 0) < 0 && errno == ESRCH))
  77     return false;
  78   return true;
  79 }
  80 
  81 /* Read the utmp entries corresponding to file FILE into freshly-
  82    malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
  83    the number of entries, and return zero.  If there is any error,
  84    return -1, setting errno, and don't modify the parameters.
  85    If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose
  86    process-IDs do not currently exist.  */
  87 
  88 #ifdef UTMP_NAME_FUNCTION
  89 
  90 int
  91 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
     /* [previous][next][first][last][top][bottom][index][help] */
  92            int options)
  93 {
  94   idx_t n_read = 0;
  95   idx_t n_alloc = 0;
  96   STRUCT_UTMP *utmp = NULL;
  97   STRUCT_UTMP *u;
  98 
  99   /* Ignore the return value for now.
 100      Solaris' utmpname returns 1 upon success -- which is contrary
 101      to what the GNU libc version does.  In addition, older GNU libc
 102      versions are actually void.   */
 103   UTMP_NAME_FUNCTION ((char *) file);
 104 
 105   SET_UTMP_ENT ();
 106 
 107   while ((u = GET_UTMP_ENT ()) != NULL)
 108     if (desirable_utmp_entry (u, options))
 109       {
 110         if (n_read == n_alloc)
 111           utmp = xpalloc (utmp, &n_alloc, 1, -1, sizeof *utmp);
 112 
 113         utmp[n_read++] = *u;
 114       }
 115 
 116   END_UTMP_ENT ();
 117 
 118   *n_entries = n_read;
 119   *utmp_buf = utmp;
 120 
 121   return 0;
 122 }
 123 
 124 #else
 125 
 126 int
 127 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
     /* [previous][next][first][last][top][bottom][index][help] */
 128            int options)
 129 {
 130   idx_t n_read = 0;
 131   idx_t n_alloc = 0;
 132   STRUCT_UTMP *utmp = NULL;
 133   int saved_errno;
 134   FILE *f = fopen (file, "re");
 135 
 136   if (! f)
 137     return -1;
 138 
 139   for (;;)
 140     {
 141       if (n_read == n_alloc)
 142         utmp = xpalloc (utmp, &n_alloc, 1, -1, sizeof *utmp);
 143       if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0)
 144         break;
 145       n_read += desirable_utmp_entry (&utmp[n_read], options);
 146     }
 147 
 148   saved_errno = ferror (f) ? errno : 0;
 149   if (fclose (f) != 0)
 150     saved_errno = errno;
 151   if (saved_errno != 0)
 152     {
 153       free (utmp);
 154       errno = saved_errno;
 155       return -1;
 156     }
 157 
 158   *n_entries = n_read;
 159   *utmp_buf = utmp;
 160 
 161   return 0;
 162 }
 163 
 164 #endif

/* [previous][next][first][last][top][bottom][index][help] */