File: check_first.c

package info (click to toggle)
ngetty 1.1-3.1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 392 kB
  • sloc: ansic: 1,588; makefile: 206; sh: 193; asm: 100
file content (45 lines) | stat: -rw-r--r-- 1,334 bytes parent folder | download | duplicates (5)
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
#include <sys/stat.h>
#include "sysinfo_defs.h"
#include "boottime_defs.h"

#if !defined(HAVE_SYSTEM_SYSINFO) && !defined(HAVE_SYSTEM_BOOTTIME)
int check_first(char *lock, struct stat *st) { return 0; }

#else
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
extern int get_uptime(time_t *t);

int check_first(char *lock, struct stat *st) /*EXTRACT_INCL*/{
  int k;
  /* If a stat error other than "no such file" occurs, I don't
     know what went wrong, so I'll proceed with caution by
     denying the autologin request. */
  if ((k=stat(lock, st)) && errno != ENOENT)
    return 0;

  if (k==0) {
    time_t uptime;
    if (get_uptime(&uptime))
      return 0;
 
    /* If there's been an autologin granted since the last boot,
       deny this and any subsequent attempts.  Note that this test
       is skipped if the LOCK file doesn't exist. */
    if (time(0) - uptime < st->st_mtime)
      return 0;
  }

  /* Create the LOCK file.  The mtime of this file provides
     a persistent record of the last time that an autologin
     request was granted.  Deny the autologin request if either
     the file open or file close fails. */
  if ((k=open(lock, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0 || close(k))
    return 0;

  /* All tests are okay, so grant the autologin request. */
  return 1;
}
#endif