summaryrefslogtreecommitdiff
path: root/libc/misc/utmp/utent.c
blob: 57719ba686b9763ad7aa53dd0179ded21aa7811d (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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* utent.c <ndf@linux.mit.edu> */
/* Let it be known that this is very possibly the worst standard ever.  HP-UX
   does one thing, someone else does another, linux another... If anyone
   actually has the standard, please send it to me.

   Note that because of the way this stupid stupid standard works, you
   have to call endutent() to close the file even if you've not called
   setutent -- getutid and family use the same file descriptor. 

   Modified by Erik Andersen for uClibc...
*/

#include <features.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <paths.h>
#include <errno.h>
#include <string.h>
#include <utmp.h>

libc_hidden_proto(strcmp)
libc_hidden_proto(strdup)
libc_hidden_proto(strncmp)
libc_hidden_proto(read)
libc_hidden_proto(write)
libc_hidden_proto(open)
libc_hidden_proto(fcntl)
libc_hidden_proto(close)
libc_hidden_proto(lseek)

#ifdef __UCLIBC_HAS_THREADS__
# include <pthread.h>
static pthread_mutex_t utmplock = PTHREAD_MUTEX_INITIALIZER;
#endif
#define LOCK	__pthread_mutex_lock(&utmplock)
#define UNLOCK	__pthread_mutex_unlock(&utmplock)



/* Some global crap */
static int static_fd = -1;
static struct utmp static_utmp;
static const char default_file_name[] = _PATH_UTMP;
static const char *static_ut_name = (const char *) default_file_name;

/* This function must be called with the LOCK held */
static void __setutent(void)
{
    int ret;

    if (static_fd == -1) {
	if ((static_fd = open(static_ut_name, O_RDWR)) < 0) {
	    if ((static_fd = open(static_ut_name, O_RDONLY)) < 0) {
		goto bummer;
	    }
	}
	/* Make sure the file will be closed on exec()  */
	ret = fcntl(static_fd, F_GETFD, 0);
	if (ret >= 0) {
	    ret = fcntl(static_fd, F_SETFD, ret | FD_CLOEXEC);
	}
	if (ret < 0) {
bummer:
	    static_fd = -1;
	    close(static_fd);
	    return;
	}
    }
    lseek(static_fd, 0, SEEK_SET);
    return;
}

libc_hidden_proto(setutent)
void setutent(void)
{
    LOCK;
    __setutent();
    UNLOCK;
}
libc_hidden_def(setutent)

/* This function must be called with the LOCK held */
static struct utmp *__getutent(int utmp_fd)
{
    struct utmp *ret = NULL;

    if (utmp_fd == -1) {
	__setutent();
    }
    if (utmp_fd == -1) {
	return NULL;
    }

    if (read(utmp_fd, (char *) &static_utmp, sizeof(struct utmp)) == sizeof(struct utmp)) 
    {
	ret = &static_utmp;
    }

    return ret;
}

void endutent(void)
{
    LOCK;
    if (static_fd != -1)
	close(static_fd);
    static_fd = -1;
    UNLOCK;
}

struct utmp *getutent(void)
{
    struct utmp *ret = NULL;

    LOCK;
    ret = __getutent(static_fd);
    UNLOCK;

    return ret;
}

/* This function must be called with the LOCK held */
static struct utmp *__getutid(const struct utmp *utmp_entry)
{
    struct utmp *lutmp;

    while ((lutmp = __getutent(static_fd)) != NULL) {
	if (	(utmp_entry->ut_type == RUN_LVL ||
		 utmp_entry->ut_type == BOOT_TIME ||
		 utmp_entry->ut_type == NEW_TIME ||
		 utmp_entry->ut_type == OLD_TIME) &&
		lutmp->ut_type == utmp_entry->ut_type)  
	{
	    return lutmp;
	}
	if (	(utmp_entry->ut_type == INIT_PROCESS ||
		 utmp_entry->ut_type == DEAD_PROCESS ||
		 utmp_entry->ut_type == LOGIN_PROCESS ||
		 utmp_entry->ut_type == USER_PROCESS) &&
		!strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id))) 
	{
	    return lutmp;
	}
    }

    return NULL;
}

libc_hidden_proto(getutid)
struct utmp *getutid(const struct utmp *utmp_entry)
{
    struct utmp *ret = NULL;

    LOCK;
    ret = __getutid(utmp_entry);
    UNLOCK;

    return ret;
}
libc_hidden_def(getutid)

struct utmp *getutline(const struct utmp *utmp_entry)
{
    struct utmp *lutmp = NULL;

    LOCK;
    while ((lutmp = __getutent(static_fd)) != NULL) {
	if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
		!strcmp(lutmp->ut_line, utmp_entry->ut_line)) {
	    break;
	}
    }
    UNLOCK;

    return lutmp;
}

struct utmp *pututline (const struct utmp *utmp_entry)
{
    LOCK;
    /* Ignore the return value.  That way, if they've already positioned
       the file pointer where they want it, everything will work out. */
    lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);

    if (__getutid(utmp_entry) != NULL)
	lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
    else
	lseek(static_fd, (off_t) 0, SEEK_END);
    if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
	utmp_entry = NULL;

    UNLOCK;
    return (struct utmp *)utmp_entry;
}

int utmpname (const char *new_ut_name)
{
    LOCK;
    if (new_ut_name != NULL) {
	if (static_ut_name != default_file_name)
	    free((char *)static_ut_name);
	static_ut_name = strdup(new_ut_name);
	if (static_ut_name == NULL) {
	    /* We should probably whine about out-of-memory 
	     * errors here...  Instead just reset to the default */
	    static_ut_name = default_file_name;
	}
    }

    if (static_fd != -1)
	close(static_fd);
    static_fd = -1;
    UNLOCK;
    return 0;
}