Commit | Line | Data |
---|---|---|
2f1de3dd CW |
1 | /* update-game-score.c --- Update a score file |
2 | Copyright (C) 2002 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | /* This program is allows a game to securely and atomically update a | |
22 | score file. It should be installed setgid, owned by an appropriate | |
23 | group like `games'. | |
24 | ||
25 | Created 2002/03/22, by Colin Walters <walters@debian.org> | |
26 | */ | |
27 | ||
28 | #define _GNU_SOURCE | |
29 | ||
30 | #include <unistd.h> | |
31 | #include <errno.h> | |
32 | #include <string.h> | |
33 | #include <stdlib.h> | |
34 | #include <stdio.h> | |
35 | #include <time.h> | |
cd553ffb | 36 | #include <pwd.h> |
2f1de3dd CW |
37 | #include <ctype.h> |
38 | #include <fcntl.h> | |
39 | #include <sys/stat.h> | |
cd553ffb | 40 | #include <config.h> |
2f1de3dd CW |
41 | |
42 | #define MAX_ATTEMPTS 5 | |
43 | #define SCORE_FILE_PREFIX "/var/games/emacs/" | |
44 | ||
45 | int | |
46 | usage(int err) | |
47 | { | |
48 | fprintf(stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n"); | |
49 | fprintf(stdout, " update-game-score -h\n"); | |
50 | fprintf(stdout, " -h\t\tDisplay this help.\n"); | |
51 | fprintf(stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n"); | |
52 | fprintf(stdout, " -r\t\tSort the scores in increasing order.\n"); | |
53 | exit(err); | |
54 | } | |
55 | ||
56 | int | |
57 | lock_file(const char *filename, void **state); | |
58 | int | |
59 | unlock_file(const char *filename, void *state); | |
60 | ||
61 | struct score_entry | |
62 | { | |
63 | long score; | |
cd553ffb | 64 | char *username; |
2f1de3dd CW |
65 | char *data; |
66 | }; | |
67 | ||
68 | int | |
69 | read_scores(const char *filename, struct score_entry **scores, | |
70 | int *count); | |
71 | int | |
72 | push_score(struct score_entry **scores, int *count, | |
cd553ffb | 73 | int newscore, char *username, char *newdata); |
2f1de3dd CW |
74 | void |
75 | sort_scores(struct score_entry *scores, int count, int reverse); | |
76 | int | |
77 | write_scores(const char *filename, const struct score_entry *scores, | |
78 | int count); | |
79 | ||
cd553ffb CW |
80 | char * |
81 | get_user_id() | |
82 | { | |
83 | char *name; | |
84 | struct passwd *buf = getpwuid(getuid()); | |
85 | if (!buf) | |
86 | { | |
87 | int count = 1; | |
88 | int uid = (int) getuid(); | |
89 | while (uid /= 10) | |
90 | count++; | |
91 | name = malloc(count+1); | |
92 | sprintf(name, "%d", uid); | |
93 | return name; | |
94 | } | |
95 | return buf->pw_name; | |
96 | } | |
97 | ||
2f1de3dd CW |
98 | int |
99 | main(int argc, char **argv) | |
100 | { | |
101 | int c; | |
102 | void *lockstate; | |
103 | char *scorefile; | |
104 | struct stat buf; | |
105 | struct score_entry *scores; | |
106 | int newscore, scorecount, reverse = 0, max = -1; | |
107 | char *newdata; | |
108 | ||
109 | srand(time(0)); | |
110 | ||
111 | while ((c = getopt(argc, argv, "hrm:")) != -1) | |
112 | switch (c) | |
113 | { | |
114 | case 'h': | |
115 | usage(0); | |
116 | break; | |
117 | case 'r': | |
118 | reverse = 1; | |
119 | break; | |
120 | case 'm': | |
121 | max = atoi(optarg); | |
122 | break; | |
123 | default: | |
124 | usage(1); | |
125 | } | |
126 | ||
127 | if (optind+3 != argc) | |
128 | usage(1); | |
cd553ffb | 129 | scorefile = malloc(strlen(SCORE_FILE_PREFIX) + strlen(argv[optind]) + 1); |
2f1de3dd CW |
130 | if (!scorefile) |
131 | { | |
132 | fprintf(stderr, "Couldn't create score file name: %s\n", | |
133 | strerror(errno)); | |
134 | goto fail; | |
135 | } | |
136 | strcpy(scorefile, SCORE_FILE_PREFIX); | |
137 | strcat(scorefile, argv[optind]); | |
138 | newscore = atoi(argv[optind+1]); | |
139 | newdata = argv[optind+2]; | |
140 | ||
141 | if (stat(scorefile, &buf) < 0) | |
142 | { | |
143 | fprintf(stderr, "Failed to access scores file \"%s\": %s\n", | |
144 | scorefile, strerror(errno)); | |
145 | goto fail; | |
146 | } | |
147 | if (lock_file(scorefile, &lockstate) < 0) | |
148 | { | |
149 | fprintf(stderr, "Failed to lock scores file \"%s\": %s\n", | |
150 | scorefile, strerror(errno)); | |
151 | goto fail; | |
152 | } | |
153 | if (read_scores(scorefile, &scores, &scorecount) < 0) | |
154 | { | |
155 | fprintf(stderr, "Failed to read scores file \"%s\": %s\n", | |
156 | scorefile, strerror(errno)); | |
157 | goto fail_unlock; | |
158 | } | |
cd553ffb | 159 | push_score(&scores, &scorecount, newscore, get_user_id(), newdata); |
2f1de3dd CW |
160 | sort_scores(scores, scorecount, reverse); |
161 | if (write_scores(scorefile, scores, scorecount) < 0) | |
162 | { | |
163 | fprintf(stderr, "Failed to write scores file \"%s\": %s\n", | |
164 | scorefile, strerror(errno)); | |
165 | goto fail_unlock; | |
166 | } | |
167 | unlock_file(scorefile, lockstate); | |
168 | exit(0); | |
169 | fail_unlock: | |
170 | unlock_file(scorefile, lockstate); | |
171 | fail: | |
172 | exit(1); | |
173 | } | |
174 | ||
175 | int | |
176 | read_score(FILE *f, struct score_entry *score) | |
177 | { | |
178 | int c; | |
179 | if (feof(f)) | |
180 | return 1; | |
181 | while ((c = getc(f)) != EOF | |
cd553ffb CW |
182 | && isdigit(c)) |
183 | { | |
184 | score->score *= 10; | |
185 | score->score += (c-48); | |
186 | } | |
187 | while ((c = getc(f)) != EOF | |
188 | && isspace(c)) | |
189 | ; | |
190 | if (c == EOF) | |
191 | return -1; | |
f5bceaf8 | 192 | ungetc(c, f); |
cd553ffb CW |
193 | #ifdef HAVE_GETDELIM |
194 | { | |
195 | int count = 0; | |
196 | if (getdelim(&score->username, &count, ' ', f) < 1 | |
197 | || score->username == NULL) | |
198 | return -1; | |
2f1de3dd | 199 | } |
cd553ffb CW |
200 | #else |
201 | { | |
202 | int unameread = 0; | |
203 | int unamelen = 30; | |
f5bceaf8 CW |
204 | char *username = malloc(unamelen); |
205 | if (!username) | |
206 | return -1; | |
cd553ffb CW |
207 | |
208 | while ((c = getc(f)) != EOF | |
209 | && !isspace(c)) | |
210 | { | |
211 | if (unameread == unamelen) | |
212 | { | |
213 | if (!(username = realloc(username, unamelen *= 2))) | |
214 | return -1; | |
215 | } | |
216 | username[unameread] = c; | |
217 | unameread++; | |
218 | } | |
f5bceaf8 CW |
219 | if (c == EOF) |
220 | return -1; | |
221 | username[unameread] = '\0'; | |
cd553ffb CW |
222 | score->username = username; |
223 | } | |
224 | #endif | |
2f1de3dd CW |
225 | #ifdef HAVE_GETLINE |
226 | score->data = NULL; | |
227 | errno = ESUCCES; | |
228 | { | |
229 | int len; | |
230 | if (getline(&score->data, &len, f) < 0) | |
231 | return -1; | |
232 | } | |
233 | #else | |
cd553ffb CW |
234 | { |
235 | int cur = 0; | |
236 | int len = 16; | |
237 | char *buf = malloc(len); | |
238 | if (!buf) | |
239 | return -1; | |
f5bceaf8 CW |
240 | while ((c = getc(f)) != EOF |
241 | && c != '\n') | |
cd553ffb CW |
242 | { |
243 | if (cur >= len-1) | |
244 | { | |
245 | if (!(buf = realloc(buf, len *= 2))) | |
246 | return -1; | |
247 | } | |
248 | buf[cur] = c; | |
249 | cur++; | |
250 | } | |
251 | score->data = buf; | |
cd553ffb | 252 | } |
2f1de3dd CW |
253 | #endif |
254 | /* Trim the newline */ | |
255 | score->data[strlen(score->data)-1] = '\0'; | |
256 | return 0; | |
257 | } | |
258 | ||
259 | int | |
260 | read_scores(const char *filename, struct score_entry **scores, | |
261 | int *count) | |
262 | { | |
263 | int readval, scorecount, cursize; | |
264 | struct score_entry *ret; | |
265 | FILE *f = fopen(filename, "r"); | |
266 | if (!f) | |
267 | return -1; | |
268 | scorecount = 0; | |
269 | cursize = 16; | |
270 | ret = malloc(sizeof(struct score_entry) * cursize); | |
271 | if (!ret) | |
272 | return -1; | |
273 | while ((readval = read_score(f, &ret[scorecount])) == 0) | |
274 | { | |
275 | /* We encoutered an error */ | |
276 | if (readval < 0) | |
277 | return -1; | |
278 | scorecount++; | |
279 | if (scorecount >= cursize) | |
280 | { | |
281 | ret = realloc(ret, cursize *= 2); | |
282 | if (!ret) | |
283 | return -1; | |
284 | } | |
285 | } | |
286 | *count = scorecount; | |
287 | *scores = ret; | |
288 | return 0; | |
289 | } | |
290 | ||
291 | int | |
292 | score_compare(const void *a, const void *b) | |
293 | { | |
294 | const struct score_entry *sa = (const struct score_entry *) a; | |
295 | const struct score_entry *sb = (const struct score_entry *) b; | |
296 | return (sb->score > sa->score) - (sb->score < sa->score); | |
297 | } | |
298 | ||
299 | int | |
300 | score_compare_reverse(const void *a, const void *b) | |
301 | { | |
302 | const struct score_entry *sa = (const struct score_entry *) a; | |
303 | const struct score_entry *sb = (const struct score_entry *) b; | |
304 | return (sa->score > sb->score) - (sa->score < sb->score); | |
305 | } | |
306 | ||
307 | int | |
308 | push_score(struct score_entry **scores, int *count, | |
cd553ffb | 309 | int newscore, char *username, char *newdata) |
2f1de3dd CW |
310 | { |
311 | struct score_entry *newscores = realloc(*scores, | |
312 | sizeof(struct score_entry) * ((*count) + 1)); | |
313 | if (!newscores) | |
314 | return -1; | |
315 | newscores[*count].score = newscore; | |
cd553ffb | 316 | newscores[*count].username = username; |
2f1de3dd CW |
317 | newscores[*count].data = newdata; |
318 | (*count) += 1; | |
319 | *scores = newscores; | |
320 | return 0; | |
321 | } | |
322 | ||
323 | void | |
324 | sort_scores(struct score_entry *scores, int count, int reverse) | |
325 | { | |
326 | qsort(scores, count, sizeof(struct score_entry), | |
327 | reverse ? score_compare_reverse : score_compare); | |
328 | } | |
329 | ||
330 | int | |
331 | write_scores(const char *filename, const struct score_entry *scores, | |
332 | int count) | |
333 | { | |
334 | FILE *f; | |
335 | int i; | |
336 | char *tempfile = malloc(strlen(filename) + strlen(".tempXXXXXX") + 1); | |
337 | if (!tempfile) | |
338 | return -1; | |
339 | strcpy(tempfile, filename); | |
340 | strcat(tempfile, ".tempXXXXXX"); | |
341 | if (mkstemp(tempfile) < 0 | |
342 | || !(f = fopen(tempfile, "w"))) | |
343 | return -1; | |
344 | for (i = 0; i < count; i++) | |
cd553ffb CW |
345 | if (fprintf(f, "%ld %s %s\n", scores[i].score, scores[i].username, |
346 | scores[i].data) < 0) | |
2f1de3dd CW |
347 | return -1; |
348 | fclose(f); | |
349 | rename(tempfile, filename); | |
350 | return 0; | |
351 | } | |
352 | ||
353 | #if 0 | |
354 | int | |
355 | lock_file(const char *filename, void **state) | |
356 | { | |
357 | struct flock lock; | |
358 | int *istate; | |
359 | int fd, attempts = 0, ret = 0; | |
360 | lock.l_type = F_WRLCK; | |
361 | lock.l_whence = SEEK_SET; | |
362 | lock.l_start = 0; | |
363 | lock.l_len = 0; | |
364 | istate = malloc(sizeof(int)); | |
365 | if (!istate) | |
366 | return -1; | |
367 | if ((fd = open(filename, O_RDWR, 0600)) < 0) | |
368 | return -1; | |
369 | *istate = fd; | |
370 | trylock: | |
371 | attempts++; | |
372 | if ((ret = fcntl(fd, F_GETLK, &lock)) == -1) | |
373 | { | |
374 | if (ret == EACCES || ret == EAGAIN) | |
375 | if (attempts > MAX_ATTEMPTS) | |
376 | exit(1); | |
377 | else | |
378 | { | |
379 | sleep((rand() % 3)+1); | |
380 | goto trylock; | |
381 | } | |
382 | else | |
383 | ret = 0 ; | |
384 | } | |
385 | else | |
386 | ret = 0; | |
387 | *state = istate; | |
388 | return ret; | |
389 | } | |
390 | ||
391 | int | |
392 | unlock_file(const char *filename, void *state) | |
393 | { | |
394 | int fd, ret; | |
395 | fd = *((int *) state); | |
396 | free(state); | |
397 | ret = close(fd); | |
398 | return ret; | |
399 | } | |
400 | ||
401 | #else | |
402 | ||
403 | int | |
404 | lock_file(const char *filename, void **state) | |
405 | { | |
406 | int fd; | |
407 | int attempts = 0; | |
408 | char *lockext = ".lockfile"; | |
409 | char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60); | |
410 | if (!lockpath) | |
411 | return -1; | |
412 | strcpy(lockpath, filename); | |
413 | strcat(lockpath, lockext); | |
414 | *state = lockpath; | |
415 | trylock: | |
416 | attempts++; | |
417 | if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 0) | |
418 | { | |
419 | if (errno == EEXIST) | |
420 | { | |
421 | /* Break the lock; we won't corrupt the file, but we might | |
422 | lose some scores. */ | |
423 | if (attempts > MAX_ATTEMPTS) | |
424 | unlink(lockpath); | |
425 | else | |
426 | sleep((rand() % 2)+1); | |
427 | goto trylock; | |
428 | } | |
429 | else | |
430 | return -1; | |
431 | } | |
432 | close(fd); | |
433 | return 0; | |
434 | } | |
435 | ||
436 | int | |
437 | unlock_file(const char *filename, void *state) | |
438 | { | |
439 | char *lockpath = (char *) state; | |
440 | int ret = unlink(lockpath); | |
441 | int saved_errno = errno; | |
442 | free(lockpath); | |
443 | errno = saved_errno; | |
444 | return ret; | |
445 | } | |
446 | ||
447 | #endif |