*** empty log message ***
[emacs.git] / lib-src / update-game-score.c
blob194997b63abec3111e4b97d89bc919cc3a126f03
1 /* update-game-score.c --- Update a score file
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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. */
21 /* This program is allows a game to securely and atomically update a
22 score file. It should be installed setuid, owned by an appropriate
23 user like `games'.
25 Alternatively, it can be compiled without HAVE_SHARED_GAME_DIR
26 defined, and in that case it will store scores in the user's home
27 directory (it should NOT be setuid).
29 Created 2002/03/22, by Colin Walters <walters@debian.org>
32 #define _GNU_SOURCE
34 #include <config.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <time.h>
42 #include <pwd.h>
43 #include <ctype.h>
44 #include <fcntl.h>
45 #include <stdarg.h>
46 #include <sys/stat.h>
48 #define MAX_ATTEMPTS 5
49 #define MAX_SCORES 200
50 #define MAX_DATA_LEN 1024
52 #if !defined (__GNUC__) || __GNUC__ < 2
53 #define __attribute__(x)
54 #endif
56 int
57 usage(int err)
59 fprintf(stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
60 fprintf(stdout, " update-game-score -h\n");
61 fprintf(stdout, " -h\t\tDisplay this help.\n");
62 fprintf(stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
63 fprintf(stdout, " -r\t\tSort the scores in increasing order.\n");
64 fprintf(stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
65 exit(err);
68 int
69 lock_file(const char *filename, void **state);
70 int
71 unlock_file(const char *filename, void *state);
73 struct score_entry
75 long score;
76 char *username;
77 char *data;
80 int
81 read_scores(const char *filename, struct score_entry **scores,
82 int *count);
83 int
84 push_score(struct score_entry **scores, int *count,
85 int newscore, char *username, char *newdata);
86 void
87 sort_scores(struct score_entry *scores, int count, int reverse);
88 int
89 write_scores(const char *filename, const struct score_entry *scores,
90 int count);
92 void lose(const char *msg, ...)
93 __attribute__ ((format (printf,1,0), noreturn));
95 void lose(const char *msg, ...)
97 va_list ap;
98 va_start(ap, msg);
99 vfprintf(stderr, msg, ap);
100 va_end(ap);
101 exit(1);
104 char *
105 get_user_id(void)
107 char *name;
108 struct passwd *buf = getpwuid(getuid());
109 if (!buf)
111 int count = 1;
112 int uid = (int) getuid();
113 int tuid = uid;
114 while (tuid /= 10)
115 count++;
116 name = malloc(count+1);
117 if (!name)
118 return NULL;
119 sprintf(name, "%d", uid);
120 return name;
122 return buf->pw_name;
125 char *
126 get_prefix(int running_suid, char *user_prefix)
128 if (!running_suid && user_prefix == NULL)
129 lose("Not using a shared game directory, and no prefix given.\n");
130 if (running_suid)
132 #ifdef HAVE_SHARED_GAME_DIR
133 return HAVE_SHARED_GAME_DIR;
134 #else
135 lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.\n");
136 #endif
138 return user_prefix;
142 main(int argc, char **argv)
144 int c, running_suid;
145 void *lockstate;
146 char *user_id, *scorefile, *prefix, *user_prefix = NULL;
147 struct stat buf;
148 struct score_entry *scores;
149 int newscore, scorecount, reverse = 0, max = MAX_SCORES;
150 char *newdata;
152 srand(time(0));
154 while ((c = getopt(argc, argv, "hrm:d:")) != -1)
155 switch (c)
157 case 'h':
158 usage(0);
159 break;
160 case 'd':
161 user_prefix = optarg;
162 break;
163 case 'r':
164 reverse = 1;
165 break;
166 case 'm':
167 max = atoi(optarg);
168 if (max > MAX_SCORES)
169 max = MAX_SCORES;
170 break;
171 default:
172 usage(1);
175 if (optind+3 != argc)
176 usage(1);
178 running_suid = (getuid() != geteuid());
180 prefix = get_prefix(running_suid, user_prefix);
182 scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2);
183 if (!scorefile)
184 lose("Couldn't create score file name: %s\n", strerror(errno));
186 strcpy(scorefile, prefix);
187 strcat(scorefile, "/");
188 strcat(scorefile, argv[optind]);
189 newscore = atoi(argv[optind+1]);
190 newdata = argv[optind+2];
191 if (strlen(newdata) > MAX_DATA_LEN)
192 newdata[MAX_DATA_LEN] = '\0';
194 if ((user_id = get_user_id()) == NULL)
195 lose("Couldn't determine user id: %s\n", strerror(errno));
197 if (stat(scorefile, &buf) < 0)
198 lose("Failed to access scores file \"%s\": %s\n", scorefile,
199 strerror(errno));
200 if (lock_file(scorefile, &lockstate) < 0)
201 lose("Failed to lock scores file \"%s\": %s\n",
202 scorefile, strerror(errno));
203 if (read_scores(scorefile, &scores, &scorecount) < 0)
205 unlock_file(scorefile, lockstate);
206 lose("Failed to read scores file \"%s\": %s\n", scorefile,
207 strerror(errno));
209 push_score(&scores, &scorecount, newscore, user_id, newdata);
210 /* Limit the number of scores. If we're using reverse sorting, then
211 we should increment the beginning of the array, to skip over the
212 *smallest* scores. Otherwise, we just decrement the number of
213 scores, since the smallest will be at the end. */
214 if (scorecount > MAX_SCORES)
215 scorecount -= (scorecount - MAX_SCORES);
216 if (reverse)
217 scores += (scorecount - MAX_SCORES);
218 sort_scores(scores, scorecount, reverse);
219 if (write_scores(scorefile, scores, scorecount) < 0)
221 unlock_file(scorefile, lockstate);
222 lose("Failed to write scores file \"%s\": %s\n", scorefile,
223 strerror(errno));
225 unlock_file(scorefile, lockstate);
226 exit(0);
230 read_score(FILE *f, struct score_entry *score)
232 int c;
233 if (feof(f))
234 return 1;
235 while ((c = getc(f)) != EOF
236 && isdigit(c))
238 score->score *= 10;
239 score->score += (c-48);
241 while ((c = getc(f)) != EOF
242 && isspace(c))
244 if (c == EOF)
245 return -1;
246 ungetc(c, f);
247 #ifdef HAVE_GETDELIM
249 size_t count = 0;
250 if (getdelim(&score->username, &count, ' ', f) < 1
251 || score->username == NULL)
252 return -1;
253 /* Trim the space */
254 score->username[strlen(score->username)-1] = '\0';
256 #else
258 int unameread = 0;
259 int unamelen = 30;
260 char *username = malloc(unamelen);
261 if (!username)
262 return -1;
264 while ((c = getc(f)) != EOF
265 && !isspace(c))
267 if (unameread >= unamelen-1)
268 if (!(username = realloc(username, unamelen *= 2)))
269 return -1;
270 username[unameread] = c;
271 unameread++;
273 if (c == EOF)
274 return -1;
275 username[unameread] = '\0';
276 score->username = username;
278 #endif
279 #ifdef HAVE_GETLINE
280 score->data = NULL;
281 errno = 0;
283 size_t len;
284 if (getline(&score->data, &len, f) < 0)
285 return -1;
286 score->data[strlen(score->data)-1] = '\0';
288 #else
290 int cur = 0;
291 int len = 16;
292 char *buf = malloc(len);
293 if (!buf)
294 return -1;
295 while ((c = getc(f)) != EOF
296 && c != '\n')
298 if (cur >= len-1)
300 if (!(buf = realloc(buf, len *= 2)))
301 return -1;
303 buf[cur] = c;
304 cur++;
306 score->data = buf;
307 score->data[cur] = '\0';
309 #endif
310 return 0;
314 read_scores(const char *filename, struct score_entry **scores,
315 int *count)
317 int readval, scorecount, cursize;
318 struct score_entry *ret;
319 FILE *f = fopen(filename, "r");
320 if (!f)
321 return -1;
322 scorecount = 0;
323 cursize = 16;
324 ret = malloc(sizeof(struct score_entry) * cursize);
325 if (!ret)
326 return -1;
327 while ((readval = read_score(f, &ret[scorecount])) == 0)
329 /* We encoutered an error */
330 if (readval < 0)
331 return -1;
332 scorecount++;
333 if (scorecount >= cursize)
335 ret = realloc(ret, cursize *= 2);
336 if (!ret)
337 return -1;
340 *count = scorecount;
341 *scores = ret;
342 return 0;
346 score_compare(const void *a, const void *b)
348 const struct score_entry *sa = (const struct score_entry *) a;
349 const struct score_entry *sb = (const struct score_entry *) b;
350 return (sb->score > sa->score) - (sb->score < sa->score);
354 score_compare_reverse(const void *a, const void *b)
356 const struct score_entry *sa = (const struct score_entry *) a;
357 const struct score_entry *sb = (const struct score_entry *) b;
358 return (sa->score > sb->score) - (sa->score < sb->score);
362 push_score(struct score_entry **scores, int *count,
363 int newscore, char *username, char *newdata)
365 struct score_entry *newscores = realloc(*scores,
366 sizeof(struct score_entry) * ((*count) + 1));
367 if (!newscores)
368 return -1;
369 newscores[*count].score = newscore;
370 newscores[*count].username = username;
371 newscores[*count].data = newdata;
372 (*count) += 1;
373 *scores = newscores;
374 return 0;
377 void
378 sort_scores(struct score_entry *scores, int count, int reverse)
380 qsort(scores, count, sizeof(struct score_entry),
381 reverse ? score_compare_reverse : score_compare);
385 write_scores(const char *filename, const struct score_entry *scores,
386 int count)
388 FILE *f;
389 int i;
390 char *tempfile = malloc(strlen(filename) + strlen(".tempXXXXXX") + 1);
391 if (!tempfile)
392 return -1;
393 strcpy(tempfile, filename);
394 strcat(tempfile, ".tempXXXXXX");
395 #ifdef HAVE_MKSTEMP
396 if (mkstemp(tempfile) < 0
397 #else
398 if (mktemp(tempfile) != tempfile
399 #endif
400 || !(f = fopen(tempfile, "w")))
401 return -1;
402 for (i = 0; i < count; i++)
403 if (fprintf(f, "%ld %s %s\n", scores[i].score, scores[i].username,
404 scores[i].data) < 0)
405 return -1;
406 fclose(f);
407 if (rename(tempfile, filename) < 0)
408 return -1;
409 if (chmod(filename, 0644) < 0)
410 return -1;
411 return 0;
415 lock_file(const char *filename, void **state)
417 int fd;
418 struct stat buf;
419 int attempts = 0;
420 char *lockext = ".lockfile";
421 char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60);
422 if (!lockpath)
423 return -1;
424 strcpy(lockpath, filename);
425 strcat(lockpath, lockext);
426 *state = lockpath;
427 trylock:
428 attempts++;
429 /* If the lock is over an hour old, delete it. */
430 if (stat(lockpath, &buf) == 0
431 && (difftime(buf.st_ctime, time(NULL) > 60*60)))
432 unlink(lockpath);
433 if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 0)
435 if (errno == EEXIST)
437 /* Break the lock; we won't corrupt the file, but we might
438 lose some scores. */
439 if (attempts > MAX_ATTEMPTS)
441 unlink(lockpath);
442 attempts = 0;
444 sleep((rand() % 2)+1);
445 goto trylock;
447 else
448 return -1;
450 close(fd);
451 return 0;
455 unlock_file(const char *filename, void *state)
457 char *lockpath = (char *) state;
458 int ret = unlink(lockpath);
459 int saved_errno = errno;
460 free(lockpath);
461 errno = saved_errno;
462 return ret;