summaryrefslogtreecommitdiffstats
path: root/src/account/aux_lu.c
blob: 9da52563267166f780054665424bd50c367ad0ce (plain)
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
#include "aux_lu.h"
#include <libuser/entity.h>
#include <libuser/user.h>
#include <utmp.h>
#include <string.h>

/*
 * Get the value of type from the given entity
 * returns string
 */
char* aux_lu_get_str(struct lu_ent* ent, char* type)
{
  return g_value_get_string(g_value_array_get_nth(lu_ent_get(ent, type), 0));
}

/*
 * Get the value of type from the given entity
 * returns long int
 */
long aux_lu_get_long(struct lu_ent* ent, char* type)
{
  return g_value_get_long(g_value_array_get_nth(lu_ent_get(ent, type), 0));
}

/*
 * Get the latest login time for given user name
 * return value greater than 0 on success
 * return 0 when not found
 * return -1 on error and errno properly set
 */
time_t aux_utmp_latest(const char* name)
{
  time_t last = 0, check = 0;
  unsigned int found = 0;
  struct utmp *rec = NULL;
  if (utmpname(WTMP_FILE))
    {
      return -1;
    }
  setutent();
  while ((rec = getutent()) != NULL)
    {
      if (rec->ut_type == USER_PROCESS && strcmp(rec->ut_user, name) == 0)
        {
          found = 1;
          check = rec->ut_tv.tv_sec;
          last = last > check ? last : check;
        }
    }
  endutent();
  return found ? last : -1;
}