View | Details | Raw Unified | Return to bug 233578
Collapse All | Expand All

(-)b/lib/libc/gen/pututxline.c (-13 / +30 lines)
Lines 43-68 __FBSDID("$FreeBSD$"); Link Here
43
#include "un-namespace.h"
43
#include "un-namespace.h"
44
44
45
static FILE *
45
static FILE *
46
futx_open(const char *file)
46
futx_open(const char *file, const char *lkfile, int *lkfd_out)
47
{
47
{
48
	FILE *fp;
48
	FILE *fp;
49
	struct stat sb;
49
	struct stat sb;
50
	int fd;
50
	int fd, lkfd, serrno;
51
52
	fd = -1;
53
	fp = NULL;
54
	serrno = 0;
55
56
	lkfd = _open(lkfile, O_CREAT|O_RDWR|O_EXLOCK|O_CLOEXEC, 0600);
57
	if (lkfd < 0)
58
		return (NULL);
51
59
52
	fd = _open(file, O_CREAT|O_RDWR|O_EXLOCK|O_CLOEXEC, 0644);
60
	fd = _open(file, O_CREAT|O_RDWR|O_EXLOCK|O_CLOEXEC, 0644);
53
	if (fd < 0)
61
	if (fd < 0)
54
		return (NULL);
62
		goto out;
55
63
56
	/* Safety check: never use broken files. */
64
	/* Safety check: never use broken files. */
57
	if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) {
65
	if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) {
58
		_close(fd);
66
		serrno = EFTYPE;
59
		errno = EFTYPE;
67
		goto out;
60
		return (NULL);
61
	}
68
	}
62
69
63
	fp = fdopen(fd, "r+");
70
	fp = fdopen(fd, "r+");
71
	*lkfd_out = lkfd;
72
out:
64
	if (fp == NULL) {
73
	if (fp == NULL) {
65
		_close(fd);
74
		if (fd >= 0)
75
			_close(fd);
76
		if (lkfd >= 0)
77
			_close(lkfd);
78
		if (serrno != 0)
79
			errno = serrno;
66
		return (NULL);
80
		return (NULL);
67
	}
81
	}
68
	return (fp);
82
	return (fp);
Lines 74-80 utx_active_add(const struct futx *fu) Link Here
74
	FILE *fp;
88
	FILE *fp;
75
	struct futx fe;
89
	struct futx fe;
76
	off_t partial;
90
	off_t partial;
77
	int error, ret;
91
	int error, ret, lkfd;
78
92
79
	partial = -1;
93
	partial = -1;
80
	ret = 0;
94
	ret = 0;
Lines 83-89 utx_active_add(const struct futx *fu) Link Here
83
	 * Register user login sessions.  Overwrite entries of sessions
97
	 * Register user login sessions.  Overwrite entries of sessions
84
	 * that have already been terminated.
98
	 * that have already been terminated.
85
	 */
99
	 */
86
	fp = futx_open(_PATH_UTX_ACTIVE);
100
	fp = futx_open(_PATH_UTX_ACTIVE, _PATH_UTX_ACTIVE_LK, &lkfd);
87
	if (fp == NULL)
101
	if (fp == NULL)
88
		return (-1);
102
		return (-1);
89
	while (fread(&fe, sizeof(fe), 1, fp) == 1) {
103
	while (fread(&fe, sizeof(fe), 1, fp) == 1) {
Lines 133-138 utx_active_add(const struct futx *fu) Link Here
133
	else
147
	else
134
		error = 0;
148
		error = 0;
135
	fclose(fp);
149
	fclose(fp);
150
	_close(lkfd);
136
	if (error != 0)
151
	if (error != 0)
137
		errno = error;
152
		errno = error;
138
	return (error == 0 ? 0 : 1);
153
	return (error == 0 ? 0 : 1);
Lines 143-154 utx_active_remove(struct futx *fu) Link Here
143
{
158
{
144
	FILE *fp;
159
	FILE *fp;
145
	struct futx fe;
160
	struct futx fe;
146
	int error, ret;
161
	int error, ret, lkfd;
147
162
148
	/*
163
	/*
149
	 * Remove user login sessions, having the same ut_id.
164
	 * Remove user login sessions, having the same ut_id.
150
	 */
165
	 */
151
	fp = futx_open(_PATH_UTX_ACTIVE);
166
	fp = futx_open(_PATH_UTX_ACTIVE, _PATH_UTX_ACTIVE_LK, &lkfd);
152
	if (fp == NULL)
167
	if (fp == NULL)
153
		return (-1);
168
		return (-1);
154
	error = ESRCH;
169
	error = ESRCH;
Lines 172-177 utx_active_remove(struct futx *fu) Link Here
172
		}
187
		}
173
188
174
	fclose(fp);
189
	fclose(fp);
190
	_close(lkfd);
175
	if (ret != 0)
191
	if (ret != 0)
176
		errno = error;
192
		errno = error;
177
	return (ret);
193
	return (ret);
Lines 202-208 utx_lastlogin_add(const struct futx *fu) Link Here
202
{
218
{
203
	struct futx fe;
219
	struct futx fe;
204
	FILE *fp;
220
	FILE *fp;
205
	int error, ret;
221
	int error, ret, lkfd;
206
222
207
	ret = 0;
223
	ret = 0;
208
224
Lines 211-217 utx_lastlogin_add(const struct futx *fu) Link Here
211
	 * current user already has an entry.  If not, append a new
227
	 * current user already has an entry.  If not, append a new
212
	 * entry.
228
	 * entry.
213
	 */
229
	 */
214
	fp = futx_open(_PATH_UTX_LASTLOGIN);
230
	fp = futx_open(_PATH_UTX_LASTLOGIN, _PATH_UTX_LASTLOGIN_LK, &lkfd);
215
	if (fp == NULL)
231
	if (fp == NULL)
216
		return (-1);
232
		return (-1);
217
	while (fread(&fe, sizeof fe, 1, fp) == 1) {
233
	while (fread(&fe, sizeof fe, 1, fp) == 1) {
Lines 229-234 utx_lastlogin_add(const struct futx *fu) Link Here
229
		ret = -1;
245
		ret = -1;
230
	}
246
	}
231
	fclose(fp);
247
	fclose(fp);
248
	_close(lkfd);
232
	if (ret == -1)
249
	if (ret == -1)
233
		errno = error;
250
		errno = error;
234
	return (ret);
251
	return (ret);
(-)b/lib/libc/gen/utxdb.h (+3 lines)
Lines 34-41 Link Here
34
#include <stdint.h>
34
#include <stdint.h>
35
35
36
#define	_PATH_UTX_ACTIVE	"/var/run/utx.active"
36
#define	_PATH_UTX_ACTIVE	"/var/run/utx.active"
37
#define	_PATH_UTX_ACTIVE_LK	"/var/run/.utx.active.lock"
37
#define	_PATH_UTX_LASTLOGIN	"/var/log/utx.lastlogin"
38
#define	_PATH_UTX_LASTLOGIN	"/var/log/utx.lastlogin"
39
#define	_PATH_UTX_LASTLOGIN_LK	"/var/log/.utx.lastlogin.lock"
38
#define	_PATH_UTX_LOG		"/var/log/utx.log"
40
#define	_PATH_UTX_LOG		"/var/log/utx.log"
41
#define	_PATH_UTX_LOG_LK	"/var/log/.utx.log.lock"
39
42
40
/*
43
/*
41
 * Entries in struct futx are ordered by how often they are used.  In
44
 * Entries in struct futx are ordered by how often they are used.  In

Return to bug 233578