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; | |
192 | #ifdef HAVE_GETDELIM | |
193 | { | |
194 | int count = 0; | |
195 | if (getdelim(&score->username, &count, ' ', f) < 1 | |
196 | || score->username == NULL) | |
197 | return -1; | |
2f1de3dd | 198 | } |
cd553ffb CW |
199 | #else |
200 | { | |
201 | int unameread = 0; | |
202 | int unamelen = 30; | |
203 | char *username; | |
204 | ||
205 | while ((c = getc(f)) != EOF | |
206 | && !isspace(c)) | |
207 | { | |
208 | if (unameread == unamelen) | |
209 | { | |
210 | if (!(username = realloc(username, unamelen *= 2))) | |
211 | return -1; | |
212 | } | |
213 | username[unameread] = c; | |
214 | unameread++; | |
215 | } | |
216 | score->username = username; | |
217 | } | |
218 | #endif | |
2f1de3dd CW |
219 | #ifdef HAVE_GETLINE |
220 | score->data = NULL; | |
221 | errno = ESUCCES; | |
222 | { | |
223 | int len; | |
224 | if (getline(&score->data, &len, f) < 0) | |
225 | return -1; | |
226 | } | |
227 | #else | |
cd553ffb CW |
228 | { |
229 | int cur = 0; | |
230 | int len = 16; | |
231 | char *buf = malloc(len); | |
232 | if (!buf) | |
233 | return -1; | |
234 | while ((c = getc(f)) != EOF) | |
235 | { | |
236 | if (cur >= len-1) | |
237 | { | |
238 | if (!(buf = realloc(buf, len *= 2))) | |
239 | return -1; | |
240 | } | |
241 | buf[cur] = c; | |
242 | cur++; | |
243 | } | |
244 | score->data = buf; | |
245 | score->data[cur+1] = '\0'; | |
246 | } | |
2f1de3dd CW |
247 | #endif |
248 | /* Trim the newline */ | |
249 | score->data[strlen(score->data)-1] = '\0'; | |
250 | return 0; | |
251 | } | |
252 | ||
253 | int | |
254 | read_scores(const char *filename, struct score_entry **scores, | |
255 | int *count) | |
256 | { | |
257 | int readval, scorecount, cursize; | |
258 | struct score_entry *ret; | |
259 | FILE *f = fopen(filename, "r"); | |
260 | if (!f) | |
261 | return -1; | |
262 | scorecount = 0; | |
263 | cursize = 16; | |
264 | ret = malloc(sizeof(struct score_entry) * cursize); | |
265 | if (!ret) | |
266 | return -1; | |
267 | while ((readval = read_score(f, &ret[scorecount])) == 0) | |
268 | { | |
269 | /* We encoutered an error */ | |
270 | if (readval < 0) | |
271 | return -1; | |
272 | scorecount++; | |
273 | if (scorecount >= cursize) | |
274 | { | |
275 | ret = realloc(ret, cursize *= 2); | |
276 | if (!ret) | |
277 | return -1; | |
278 | } | |
279 | } | |
280 | *count = scorecount; | |
281 | *scores = ret; | |
282 | return 0; | |
283 | } | |
284 | ||
285 | int | |
286 | score_compare(const void *a, const void *b) | |
287 | { | |
288 | const struct score_entry *sa = (const struct score_entry *) a; | |
289 | const struct score_entry *sb = (const struct score_entry *) b; | |
290 | return (sb->score > sa->score) - (sb->score < sa->score); | |
291 | } | |
292 | ||
293 | int | |
294 | score_compare_reverse(const void *a, const void *b) | |
295 | { | |
296 | const struct score_entry *sa = (const struct score_entry *) a; | |
297 | const struct score_entry *sb = (const struct score_entry *) b; | |
298 | return (sa->score > sb->score) - (sa->score < sb->score); | |
299 | } | |
300 | ||
301 | int | |
302 | push_score(struct score_entry **scores, int *count, | |
cd553ffb | 303 | int newscore, char *username, char *newdata) |
2f1de3dd CW |
304 | { |
305 | struct score_entry *newscores = realloc(*scores, | |
306 | sizeof(struct score_entry) * ((*count) + 1)); | |
307 | if (!newscores) | |
308 | return -1; | |
309 | newscores[*count].score = newscore; | |
cd553ffb | 310 | newscores[*count].username = username; |
2f1de3dd CW |
311 | newscores[*count].data = newdata; |
312 | (*count) += 1; | |
313 | *scores = newscores; | |
314 | return 0; | |
315 | } | |
316 | ||
317 | void | |
318 | sort_scores(struct score_entry *scores, int count, int reverse) | |
319 | { | |
320 | qsort(scores, count, sizeof(struct score_entry), | |
321 | reverse ? score_compare_reverse : score_compare); | |
322 | } | |
323 | ||
324 | int | |
325 | write_scores(const char *filename, const struct score_entry *scores, | |
326 | int count) | |
327 | { | |
328 | FILE *f; | |
329 | int i; | |
330 | char *tempfile = malloc(strlen(filename) + strlen(".tempXXXXXX") + 1); | |
331 | if (!tempfile) | |
332 | return -1; | |
333 | strcpy(tempfile, filename); | |
334 | strcat(tempfile, ".tempXXXXXX"); | |
335 | if (mkstemp(tempfile) < 0 | |
336 | || !(f = fopen(tempfile, "w"))) | |
337 | return -1; | |
338 | for (i = 0; i < count; i++) | |
cd553ffb CW |
339 | if (fprintf(f, "%ld %s %s\n", scores[i].score, scores[i].username, |
340 | scores[i].data) < 0) | |
2f1de3dd CW |
341 | return -1; |
342 | fclose(f); | |
343 | rename(tempfile, filename); | |
344 | return 0; | |
345 | } | |
346 | ||
347 | #if 0 | |
348 | int | |
349 | lock_file(const char *filename, void **state) | |
350 | { | |
351 | struct flock lock; | |
352 | int *istate; | |
353 | int fd, attempts = 0, ret = 0; | |
354 | lock.l_type = F_WRLCK; | |
355 | lock.l_whence = SEEK_SET; | |
356 | lock.l_start = 0; | |
357 | lock.l_len = 0; | |
358 | istate = malloc(sizeof(int)); | |
359 | if (!istate) | |
360 | return -1; | |
361 | if ((fd = open(filename, O_RDWR, 0600)) < 0) | |
362 | return -1; | |
363 | *istate = fd; | |
364 | trylock: | |
365 | attempts++; | |
366 | if ((ret = fcntl(fd, F_GETLK, &lock)) == -1) | |
367 | { | |
368 | if (ret == EACCES || ret == EAGAIN) | |
369 | if (attempts > MAX_ATTEMPTS) | |
370 | exit(1); | |
371 | else | |
372 | { | |
373 | sleep((rand() % 3)+1); | |
374 | goto trylock; | |
375 | } | |
376 | else | |
377 | ret = 0 ; | |
378 | } | |
379 | else | |
380 | ret = 0; | |
381 | *state = istate; | |
382 | return ret; | |
383 | } | |
384 | ||
385 | int | |
386 | unlock_file(const char *filename, void *state) | |
387 | { | |
388 | int fd, ret; | |
389 | fd = *((int *) state); | |
390 | free(state); | |
391 | ret = close(fd); | |
392 | return ret; | |
393 | } | |
394 | ||
395 | #else | |
396 | ||
397 | int | |
398 | lock_file(const char *filename, void **state) | |
399 | { | |
400 | int fd; | |
401 | int attempts = 0; | |
402 | char *lockext = ".lockfile"; | |
403 | char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60); | |
404 | if (!lockpath) | |
405 | return -1; | |
406 | strcpy(lockpath, filename); | |
407 | strcat(lockpath, lockext); | |
408 | *state = lockpath; | |
409 | trylock: | |
410 | attempts++; | |
411 | if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 0) | |
412 | { | |
413 | if (errno == EEXIST) | |
414 | { | |
415 | /* Break the lock; we won't corrupt the file, but we might | |
416 | lose some scores. */ | |
417 | if (attempts > MAX_ATTEMPTS) | |
418 | unlink(lockpath); | |
419 | else | |
420 | sleep((rand() % 2)+1); | |
421 | goto trylock; | |
422 | } | |
423 | else | |
424 | return -1; | |
425 | } | |
426 | close(fd); | |
427 | return 0; | |
428 | } | |
429 | ||
430 | int | |
431 | unlock_file(const char *filename, void *state) | |
432 | { | |
433 | char *lockpath = (char *) state; | |
434 | int ret = unlink(lockpath); | |
435 | int saved_errno = errno; | |
436 | free(lockpath); | |
437 | errno = saved_errno; | |
438 | return ret; | |
439 | } | |
440 | ||
441 | #endif |