File: utmp.c

package info (click to toggle)
rxvt 1%3A2.4.5-16
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,128 kB
  • ctags: 1,296
  • sloc: ansic: 12,852; makefile: 521; sh: 411
file content (284 lines) | stat: -rw-r--r-- 7,563 bytes parent folder | download | duplicates (2)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*--------------------------------*-C-*---------------------------------*
 * File:	utmp.c
 *----------------------------------------------------------------------*
 * Copyright 1997 1998 Geoff Wing <mason@primenet.com.au>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *----------------------------------------------------------------------*
 * Public:
 *	extern void cleanutent (void);
 *	extern void makeutent (const char * pty, const char * hostname);
 *
 * Private:
 *	write_utmp ();
 *	update_wtmp ();
 *----------------------------------------------------------------------*/

#include "rxvt.h"		/* NECESSARY */

/*
 * HAVE_SETUTENT corresponds to SYSV-style utmp support.
 * Without it corresponds to using BSD utmp support.
 * SYSV-style utmp support is further divided in normal utmp support
 * and utmpx support (Solaris 2.x) by HAVE_UTMPX_H
 */

/*
 * update wtmp entries - only for SYSV style UTMP systems
 */
#ifdef UTMP_SUPPORT
static char     ut_id[5];	/* remember if entry to utmp made */
# ifndef USE_SYSV_UTMP
static int      utmp_pos;	/* BSD position of utmp-stamp */
# endif
#endif

/* ------------------------------------------------------------------------- */
#ifndef HAVE_UTMPX_H		/* supposedly we have updwtmpx ? */
#ifdef WTMP_SUPPORT
/* PROTO */
void
update_wtmp(char *fname, struct utmp *putmp)
{
    int             fd, retry = 10;	/* 10 attempts at locking */
    struct flock    lck;	/* fcntl locking scheme */

    if ((fd = open(fname, O_WRONLY | O_APPEND, 0)) < 0)
	return;

    lck.l_whence = SEEK_END;	/* start lock at current eof */
    lck.l_len = 0;		/* end at ``largest possible eof'' */
    lck.l_start = 0;
    lck.l_type = F_WRLCK;	/* we want a write lock */

    while (retry--)
    /* attempt lock with F_SETLK - F_SETLKW would cause a deadlock! */
	if ((fcntl(fd, F_SETLK, &lck) < 0) && errno != EACCESS) {
	    close(fd);
	    return;		/* failed for unknown reason: give up */
	}
    write(fd, putmp, sizeof(struct utmp));

/* unlocking the file */
    lck.l_type = F_UNLCK;
    fcntl(fd, F_SETLK, &lck);

    close(fd);
}
#endif				/* WTMP_SUPPORT */
#endif				/* !HAVE_UTMPX_H */
/* ------------------------------------------------------------------------- */
#ifdef UTMP_SUPPORT
/*
 * make a utmp entry
 */
/* PROTO */
void
makeutent(const char *pty, const char *hostname)
{

    struct passwd  *pwent = getpwuid(getuid());
    UTMP            utmp;

#ifndef USE_SYSV_UTMP
/*
 * BSD style utmp entry
 *      ut_line, ut_name, ut_host, ut_time
 */
    int             rval = 0, i;
    FILE           *fd0, *fd1;
    char            buf[256], name[256];

#else
/*
 * SYSV style utmp entry
 *      ut_user, ut_id, ut_line, ut_pid, ut_type, ut_exit, ut_time
 */
    char           *colon;

#endif				/* !USE_SYSV_UTMP */

/* BSD naming is of the form /dev/tty?? or /dev/pty?? */

    rmemset(&utmp, 0, sizeof(UTMP));
    if (!strncmp(pty, "/dev/", 5))
	pty += 5;		/* skip /dev/ prefix */
    if (!strncmp(pty, "pty", 3) || !strncmp(pty, "tty", 3))
	strncpy(ut_id, (pty + 3), sizeof(ut_id));
    else
#ifndef USE_SYSV_UTMP
    {
	print_error("can't parse tty name \"%s\"", pty);
	ut_id[0] = '\0';	/* entry not made */
	return;
    }

    strncpy(utmp.ut_line, pty, sizeof(utmp.ut_line));
    strncpy(utmp.ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
	    sizeof(utmp.ut_name));
    strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
    utmp.ut_time = time(NULL);

    if ((fd0 = fopen(UTMP_FILENAME, "r+")) == NULL)
	ut_id[0] = '\0';	/* entry not made */
    else {
	utmp_pos = -1;
	if ((fd1 = fopen(TTYTAB_FILENAME, "r")) != NULL) {
	    for (i = 1; (fgets(buf, sizeof(buf), fd1) != NULL); i++) {
		if (*buf == '#' || sscanf(buf, "%s", name) != 1)
		    continue;
		if (!strcmp(utmp.ut_line, name)) {
		    fclose(fd1);
		    utmp_pos = i * sizeof(struct utmp);

		    break;
		}
	    }
	    fclose(fd1);
	}
	if (utmp_pos < 0)
	    ut_id[0] = '\0';	/* entry not made */
	else {
	    fseek(fd0, utmp_pos, 0);
	    fwrite(&utmp, sizeof(UTMP), 1, fd0);
	    rval = 1;
	}
	fclose(fd0);
    }

#else				/* USE_SYSV_UTMP */
    {
	int             n;

	if (sscanf(pty, "pts/%d", &n) == 1)
	    sprintf(ut_id, "vt%02x", (n % 256));	/* sysv naming */
	else {
	    print_error("can't parse tty name \"%s\"", pty);
	    ut_id[0] = '\0';	/* entry not made */
	    return;
	}
    }

    utmpname(UTMP_FILENAME);

    setutent();			/* XXX: should be unnecessaray */

    strncpy(utmp.ut_id, ut_id, sizeof(utmp.ut_id));
    utmp.ut_type = DEAD_PROCESS;
    (void)getutid(&utmp);	/* position to entry in utmp file */

/* set up the new entry */
    utmp.ut_type = USER_PROCESS;
#ifndef linux
    utmp.ut_exit.e_exit = 2;
#endif
    strncpy(utmp.ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
	    sizeof(utmp.ut_user));
    strncpy(utmp.ut_id, ut_id, sizeof(utmp.ut_id));
    strncpy(utmp.ut_line, pty, sizeof(utmp.ut_line));

#ifdef HAVE_UTMP_HOST
    strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
#ifndef linux
    if ((colon = strrchr(utmp.ut_host, ':')) != NULL)
	*colon = '\0';
#endif
#endif				/* HAVE_UTMP_HOST */

/* ut_name is normally the same as ut_user, but .... */
    strncpy(utmp.ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
	    sizeof(utmp.ut_name));

    utmp.ut_pid = getpid();

#ifdef HAVE_UTMPX_H
    utmp.ut_session = getsid(0);
    utmp.ut_tv.tv_sec = time(NULL);
    utmp.ut_tv.tv_usec = 0;
#else
    utmp.ut_time = time(NULL);
#endif				/* HAVE_UTMPX_H */

    pututline(&utmp);

#ifdef WTMP_SUPPORT
    update_wtmp(WTMP_FILENAME, &utmp);
#endif

    endutent();			/* close the file */
#endif				/* !USE_SYSV_UTMP */
}
#endif				/* UTMP_SUPPORT */

/* ------------------------------------------------------------------------- */
#ifdef UTMP_SUPPORT
/*
 * remove a utmp entry
 */
/* PROTO */
void
cleanutent(void)
{
    UTMP            utmp;

#ifndef USE_SYSV_UTMP
    FILE           *fd;

    if (ut_id[0] && ((fd = fopen(UTMP_FILENAME, "r+")) != NULL)) {
	rmemset(&utmp, 0, sizeof(struct utmp));

	fseek(fd, utmp_pos, 0);
	fwrite(&utmp, sizeof(struct utmp), 1, fd);

	fclose(fd);
    }
#else				/* USE_SYSV_UTMP */
    UTMP	   *putmp;
    pid_t           pid = getpid();

    if (!ut_id[0])
	return;			/* entry not made */

    utmpname(UTMP_FILENAME);
    rmemset(&utmp, 0, sizeof(UTMP));
    strncpy(utmp.ut_id, ut_id, sizeof(utmp.ut_id));
    utmp.ut_type = USER_PROCESS;

    setutent();			/* XXX: should be unnecessaray */

    putmp = getutid(&utmp);
    if (!putmp || putmp->ut_pid != pid)
	return;

    putmp->ut_type = DEAD_PROCESS;

#ifdef HAVE_UTMPX_H
    utmp.ut_session = getsid(0);
    utmp.ut_tv.tv_sec = time(NULL);
    utmp.ut_tv.tv_usec = 0;
#else				/* HAVE_UTMPX_H */
    putmp->ut_time = time(NULL);
#endif				/* HAVE_UTMPX_H */
    pututline(&utmp);

#ifdef WTMP_SUPPORT
    update_wtmp(WTMP_FILENAME, putmp);
#endif

    endutent();
#endif				/* !USE_SYSV_UTMP */
}
#endif