File: test-readutmp.c

package info (click to toggle)
coreutils 9.7-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,780 kB
  • sloc: ansic: 243,477; sh: 29,063; perl: 7,908; yacc: 1,858; makefile: 196; python: 47; sed: 16
file content (160 lines) | stat: -rw-r--r-- 5,779 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* Test of readutmp module.
   Copyright (C) 2023-2025 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2023.  */

#include <config.h>

#include "readutmp.h"

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "idx.h"
#include "xalloc.h"

#define ELEMENT STRUCT_UTMP
#define COMPARE(entry1, entry2) \
  _GL_CMP (UT_TIME_MEMBER (entry1), UT_TIME_MEMBER (entry2))
#define STATIC static
#include "array-mergesort.h"

#include "macros.h"

int
main (int argc, char *argv[])
{
  STRUCT_UTMP *entries;
  idx_t num_entries;

  if (read_utmp (UTMP_FILE, &num_entries, &entries, 0) < 0)
    {
      #if READ_UTMP_SUPPORTED
      fprintf (stderr, "Skipping test: cannot open %s\n", UTMP_FILE);
      #else
      fprintf (stderr, "Skipping test: neither <utmpx.h> nor <utmp.h> is available\n");
      #endif
      return 77;
    }

  printf ("Here are the read_utmp results.\n");
  printf ("Flags: B = Boot, U = User Process\n");
  printf ("\n");
  printf ("                                                              Termi‐      Flags\n");
  printf ("    Time (GMT)             User          Device        PID    nation Exit  B U  Host\n");
  printf ("------------------- ------------------ ----------- ---------- ------ ----  - -  ----\n");

  /* What do the results look like?
     * On Alpine Linux, Cygwin, Android, the output is empty.
     * The entries are usually not sorted according to the Time column, so
       we do it here.
     * In the User column, special values exist:
       - The empty string denotes a system event.
         Seen on glibc, macOS, FreeBSD, NetBSD, OpenBSD, AIX
       - The value "reboot" denotes a reboot.
         Seen on glibc
       - The value "runlevel" denotes a runlevel change.
         Seen on glibc
       - The value "LOGIN" denotes the start of a virtual console.
         Seen on glibc, Solaris
       - The value "/usr/libexec/getty" denotes the start of a virtual console.
         Seen on NetBSD
     * In the Device column:
       - The empty string denotes a system event.
         Seen on macOS, FreeBSD, AIX
       - The value "~" denotes an event with no associated device.
         Seen on glibc
       - The values "system boot", "system down", "run-level N" are
         seen on NetBSD, AIX, Solaris.
       - The values "old time", "new time" are
         seen on Solaris.
       - Common devices are:
         - On glibc: "ttyN" (console) and "pts/N" (pseudo-terminals).
         - On macOS: "ttysNNN" (pseudo-terminals).
         - On FreeBSD: "ttyvN" (console).
         - On NetBSD: "ttyEN", "constty" (console).
         - On OpenBSD: "ttyCN", "console" (console) and "ttypN" (pseudo-terminals).
         - on AIX: "vtyN" (console) and "pts/N" (pseudo-terminals).
         - On Solaris: "vt/N", "console" (console) and "pts/N" (pseudo-terminals).
     * The PID column is zero on platforms without a 'ut_pid' field: OpenBSD.
   */
  if (num_entries > 0)
    {
      /* Sort the entries according to increasing UT_TIME_MEMBER (entry).
         Use a stable sort algorithm.  */
      merge_sort_inplace (entries, num_entries,
                          XNMALLOC (num_entries, STRUCT_UTMP));

      idx_t boot_time_count = 0;
      idx_t i;
      for (i = 0; i < num_entries; i++)
        {
          const STRUCT_UTMP *entry = &entries[i];

          char *user = extract_trimmed_name (entry);
          const char *device = entry->ut_line;
          long pid = UT_PID (entry);
          int termination = UT_EXIT_E_TERMINATION (entry);
          int exit = UT_EXIT_E_EXIT (entry);
          const char *host = entry->ut_host;

          time_t tim = UT_TIME_MEMBER (entry);
          struct tm *gmt = gmtime (&tim);
          char timbuf[100];
          if (gmt == NULL
              || strftime (timbuf, sizeof (timbuf), "%Y-%m-%d %H:%M:%S", gmt)
                 == 0)
            strcpy (timbuf, "---");

          printf ("%-19s %-18s %-11s %10ld %4d   %3d   %c %c  %s\n",
                  timbuf,
                  user,
                  device,
                  pid,
                  termination,
                  exit,
                  UT_TYPE_BOOT_TIME (entry) ? 'X' : ' ',
                  UT_TYPE_USER_PROCESS (entry) ? 'X' : ' ',
                  host);

          if (UT_TYPE_BOOT_TIME (entry))
            boot_time_count++;
        }
      fflush (stdout);

      /* If the first time is more than 5 years in the past or the last time
         is more than a week in the future, the time_t members are wrong.  */
      time_t first = UT_TIME_MEMBER (&entries[0]);
      time_t last = UT_TIME_MEMBER (&entries[num_entries - 1]);
      time_t now = time (NULL);
      ASSERT (first >= now - 157680000);
      ASSERT (last <= now + 604800);

      /* read_utmp should not produce multiple BOOT_TIME entries.  */
      ASSERT (boot_time_count <= 1);

      /* read_utmp should fake a BOOT_TIME entry if needed.
         Platform specific hacks go into lib/boot-time-aux.h.  */
      ASSERT (boot_time_count >= 1);
    }

  free (entries);

  return test_exit_status;
}