* xfont.c (compare_font_names): Redo to omit the need for casts.
[bpt/emacs.git] / lib-src / update-game-score.c
CommitLineData
2f1de3dd 1/* update-game-score.c --- Update a score file
294981c7 2
acaf905b 3Copyright (C) 2002-2012 Free Software Foundation, Inc.
294981c7
GM
4
5Author: Colin Walters <walters@debian.org>
2f1de3dd
CW
6
7This file is part of GNU Emacs.
8
294981c7 9GNU Emacs is free software: you can redistribute it and/or modify
2f1de3dd 10it under the terms of the GNU General Public License as published by
294981c7
GM
11the Free Software Foundation, either version 3 of the License, or
12(at your option) any later version.
2f1de3dd
CW
13
14GNU Emacs is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
294981c7
GM
20along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
2f1de3dd 22
89cc1591 23/* This program allows a game to securely and atomically update a
8eec804f
CW
24 score file. It should be installed setuid, owned by an appropriate
25 user like `games'.
26
27 Alternatively, it can be compiled without HAVE_SHARED_GAME_DIR
28 defined, and in that case it will store scores in the user's home
29 directory (it should NOT be setuid).
2f1de3dd 30
294981c7 31 Created 2002/03/22.
2f1de3dd
CW
32*/
33
c42d6dbd
EZ
34#include <config.h>
35
2f1de3dd
CW
36#include <unistd.h>
37#include <errno.h>
0c6d656d 38#include <limits.h>
2f1de3dd
CW
39#include <string.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <time.h>
cd553ffb 43#include <pwd.h>
2f1de3dd 44#include <ctype.h>
5b400482 45#ifdef HAVE_FCNTL_H
2f1de3dd 46#include <fcntl.h>
5b400482 47#endif
2f1de3dd 48#include <sys/stat.h>
15142f27 49#include <getopt.h>
5b400482 50
2f1de3dd 51#define MAX_ATTEMPTS 5
8eec804f
CW
52#define MAX_SCORES 200
53#define MAX_DATA_LEN 1024
5795b420 54
1d28afaf
DL
55#ifndef HAVE_DIFFTIME
56/* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
57#define difftime(t1, t0) (double)((t1) - (t0))
58#endif
59
845ca893 60static _Noreturn void
873fbd0b 61usage (int err)
2f1de3dd 62{
ec3b5374 63 fprintf (stdout, "Usage: update-game-score [-m MAX] [-r] [-d DIR] game/scorefile SCORE DATA\n");
a5d107f3
RS
64 fprintf (stdout, " update-game-score -h\n");
65 fprintf (stdout, " -h\t\tDisplay this help.\n");
66 fprintf (stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
67 fprintf (stdout, " -r\t\tSort the scores in increasing order.\n");
68 fprintf (stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
69 exit (err);
2f1de3dd
CW
70}
71
b23b5a5b
PE
72static int lock_file (const char *filename, void **state);
73static int unlock_file (const char *filename, void *state);
2f1de3dd
CW
74
75struct score_entry
76{
77 long score;
cd553ffb 78 char *username;
2f1de3dd
CW
79 char *data;
80};
81
b23b5a5b
PE
82static int read_scores (const char *filename, struct score_entry **scores,
83 int *count);
84static int push_score (struct score_entry **scores, int *count,
85 int newscore, char *username, char *newdata);
86static void sort_scores (struct score_entry *scores, int count, int reverse);
87static int write_scores (const char *filename,
88 const struct score_entry *scores, int count);
cf398788 89
845ca893 90static _Noreturn void
873fbd0b 91lose (const char *msg)
cf398788 92{
a5d107f3 93 fprintf (stderr, "%s\n", msg);
65396510 94 exit (EXIT_FAILURE);
cf398788 95}
2f1de3dd 96
1d28afaf
DL
97/* Taken from sysdep.c. */
98#ifndef HAVE_STRERROR
99#ifndef WINDOWSNT
100char *
ec3b5374 101strerror (int errnum)
1d28afaf
DL
102{
103 extern char *sys_errlist[];
104 extern int sys_nerr;
105
106 if (errnum >= 0 && errnum < sys_nerr)
107 return sys_errlist[errnum];
108 return (char *) "Unknown error";
109}
110#endif /* not WINDOWSNT */
111#endif /* ! HAVE_STRERROR */
112
845ca893 113static _Noreturn void
873fbd0b 114lose_syserr (const char *msg)
d99fabf0 115{
a5d107f3 116 fprintf (stderr, "%s: %s\n", msg, strerror (errno));
65396510 117 exit (EXIT_FAILURE);
d99fabf0
CW
118}
119
b23b5a5b 120static char *
f57e2426 121get_user_id (void)
cd553ffb 122{
a5d107f3 123 struct passwd *buf = getpwuid (getuid ());
cd553ffb
CW
124 if (!buf)
125 {
0c6d656d
PE
126 long uid = getuid ();
127 char *name = malloc (sizeof uid * CHAR_BIT / 3 + 1);
128 if (name)
129 sprintf (name, "%ld", uid);
cd553ffb
CW
130 return name;
131 }
132 return buf->pw_name;
133}
134
b23b5a5b 135static const char *
988e88ab 136get_prefix (int running_suid, const char *user_prefix)
8eec804f 137{
d99fabf0 138 if (!running_suid && user_prefix == NULL)
a5d107f3 139 lose ("Not using a shared game directory, and no prefix given.");
d99fabf0
CW
140 if (running_suid)
141 {
142#ifdef HAVE_SHARED_GAME_DIR
143 return HAVE_SHARED_GAME_DIR;
144#else
a5d107f3 145 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
d99fabf0
CW
146#endif
147 }
148 return user_prefix;
8eec804f
CW
149}
150
2f1de3dd 151int
873fbd0b 152main (int argc, char **argv)
2f1de3dd 153{
d99fabf0 154 int c, running_suid;
2f1de3dd 155 void *lockstate;
988e88ab
J
156 char *user_id, *scorefile;
157 const char *prefix, *user_prefix = NULL;
2f1de3dd
CW
158 struct stat buf;
159 struct score_entry *scores;
8eec804f 160 int newscore, scorecount, reverse = 0, max = MAX_SCORES;
2f1de3dd
CW
161 char *newdata;
162
a5d107f3 163 srand (time (0));
2f1de3dd 164
a5d107f3 165 while ((c = getopt (argc, argv, "hrm:d:")) != -1)
2f1de3dd
CW
166 switch (c)
167 {
168 case 'h':
65396510 169 usage (EXIT_SUCCESS);
2f1de3dd 170 break;
d99fabf0
CW
171 case 'd':
172 user_prefix = optarg;
173 break;
2f1de3dd
CW
174 case 'r':
175 reverse = 1;
176 break;
177 case 'm':
a5d107f3 178 max = atoi (optarg);
8eec804f
CW
179 if (max > MAX_SCORES)
180 max = MAX_SCORES;
2f1de3dd
CW
181 break;
182 default:
65396510 183 usage (EXIT_FAILURE);
2f1de3dd
CW
184 }
185
186 if (optind+3 != argc)
65396510 187 usage (EXIT_FAILURE);
5795b420 188
a5d107f3 189 running_suid = (getuid () != geteuid ());
5795b420 190
a5d107f3 191 prefix = get_prefix (running_suid, user_prefix);
8eec804f 192
a5d107f3 193 scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2);
2f1de3dd 194 if (!scorefile)
a5d107f3 195 lose_syserr ("Couldn't allocate score file");
8eec804f 196
a5d107f3
RS
197 strcpy (scorefile, prefix);
198 strcat (scorefile, "/");
199 strcat (scorefile, argv[optind]);
200 newscore = atoi (argv[optind+1]);
2f1de3dd 201 newdata = argv[optind+2];
a5d107f3 202 if (strlen (newdata) > MAX_DATA_LEN)
8eec804f 203 newdata[MAX_DATA_LEN] = '\0';
d99fabf0 204
00a7e408
RS
205 user_id = get_user_id ();
206 if (user_id == NULL)
a5d107f3 207 lose_syserr ("Couldn't determine user id");
177c0ea7 208
a5d107f3
RS
209 if (stat (scorefile, &buf) < 0)
210 lose_syserr ("Failed to access scores file");
177c0ea7 211
a5d107f3
RS
212 if (lock_file (scorefile, &lockstate) < 0)
213 lose_syserr ("Failed to lock scores file");
177c0ea7 214
a5d107f3 215 if (read_scores (scorefile, &scores, &scorecount) < 0)
2f1de3dd 216 {
a5d107f3
RS
217 unlock_file (scorefile, lockstate);
218 lose_syserr ("Failed to read scores file");
2f1de3dd 219 }
a5d107f3 220 push_score (&scores, &scorecount, newscore, user_id, newdata);
24e9e996 221 sort_scores (scores, scorecount, reverse);
8eec804f 222 /* Limit the number of scores. If we're using reverse sorting, then
f0d80d43
PE
223 also increment the beginning of the array, to skip over the
224 *smallest* scores. Otherwise, just decrementing the number of
225 scores suffices, since the smallest is at the end. */
8eec804f 226 if (scorecount > MAX_SCORES)
f0d80d43
PE
227 {
228 if (reverse)
229 scores += (scorecount - MAX_SCORES);
230 scorecount = MAX_SCORES;
231 }
a5d107f3 232 if (write_scores (scorefile, scores, scorecount) < 0)
2f1de3dd 233 {
a5d107f3
RS
234 unlock_file (scorefile, lockstate);
235 lose_syserr ("Failed to write scores file");
2f1de3dd 236 }
a5d107f3 237 unlock_file (scorefile, lockstate);
65396510 238 exit (EXIT_SUCCESS);
2f1de3dd
CW
239}
240
b23b5a5b 241static int
873fbd0b 242read_score (FILE *f, struct score_entry *score)
2f1de3dd
CW
243{
244 int c;
a5d107f3 245 if (feof (f))
2f1de3dd 246 return 1;
a5d107f3
RS
247 while ((c = getc (f)) != EOF
248 && isdigit (c))
cd553ffb
CW
249 {
250 score->score *= 10;
251 score->score += (c-48);
252 }
a5d107f3
RS
253 while ((c = getc (f)) != EOF
254 && isspace (c))
cd553ffb
CW
255 ;
256 if (c == EOF)
257 return -1;
a5d107f3 258 ungetc (c, f);
cd553ffb
CW
259#ifdef HAVE_GETDELIM
260 {
7605f1bd 261 size_t count = 0;
a5d107f3 262 if (getdelim (&score->username, &count, ' ', f) < 1
cd553ffb
CW
263 || score->username == NULL)
264 return -1;
1d4328ff 265 /* Trim the space */
a5d107f3 266 score->username[strlen (score->username)-1] = '\0';
2f1de3dd 267 }
cd553ffb
CW
268#else
269 {
270 int unameread = 0;
271 int unamelen = 30;
a5d107f3 272 char *username = malloc (unamelen);
f5bceaf8
CW
273 if (!username)
274 return -1;
177c0ea7 275
a5d107f3
RS
276 while ((c = getc (f)) != EOF
277 && !isspace (c))
cd553ffb 278 {
8eec804f 279 if (unameread >= unamelen-1)
a5d107f3 280 if (!(username = realloc (username, unamelen *= 2)))
8eec804f 281 return -1;
cd553ffb
CW
282 username[unameread] = c;
283 unameread++;
284 }
177c0ea7 285 if (c == EOF)
f5bceaf8
CW
286 return -1;
287 username[unameread] = '\0';
cd553ffb
CW
288 score->username = username;
289 }
290#endif
2f1de3dd
CW
291#ifdef HAVE_GETLINE
292 score->data = NULL;
7605f1bd 293 errno = 0;
2f1de3dd 294 {
7605f1bd 295 size_t len;
a5d107f3 296 if (getline (&score->data, &len, f) < 0)
2f1de3dd 297 return -1;
a5d107f3 298 score->data[strlen (score->data)-1] = '\0';
2f1de3dd
CW
299 }
300#else
cd553ffb
CW
301 {
302 int cur = 0;
303 int len = 16;
a5d107f3 304 char *buf = malloc (len);
cd553ffb
CW
305 if (!buf)
306 return -1;
a5d107f3 307 while ((c = getc (f)) != EOF
f5bceaf8 308 && c != '\n')
cd553ffb
CW
309 {
310 if (cur >= len-1)
311 {
a5d107f3 312 if (!(buf = realloc (buf, len *= 2)))
cd553ffb
CW
313 return -1;
314 }
315 buf[cur] = c;
316 cur++;
317 }
318 score->data = buf;
5795b420 319 score->data[cur] = '\0';
cd553ffb 320 }
2f1de3dd 321#endif
2f1de3dd
CW
322 return 0;
323}
324
b23b5a5b 325static int
873fbd0b 326read_scores (const char *filename, struct score_entry **scores, int *count)
2f1de3dd
CW
327{
328 int readval, scorecount, cursize;
329 struct score_entry *ret;
a5d107f3 330 FILE *f = fopen (filename, "r");
177c0ea7 331 if (!f)
2f1de3dd
CW
332 return -1;
333 scorecount = 0;
334 cursize = 16;
00a7e408 335 ret = (struct score_entry *) malloc (sizeof (struct score_entry) * cursize);
177c0ea7 336 if (!ret)
2f1de3dd 337 return -1;
a5d107f3 338 while ((readval = read_score (f, &ret[scorecount])) == 0)
2f1de3dd 339 {
4c36be58 340 /* We encountered an error. */
2f1de3dd
CW
341 if (readval < 0)
342 return -1;
343 scorecount++;
344 if (scorecount >= cursize)
345 {
ee435bae
JB
346 cursize *= 2;
347 ret = (struct score_entry *)
348 realloc (ret, (sizeof (struct score_entry) * cursize));
2f1de3dd
CW
349 if (!ret)
350 return -1;
351 }
352 }
353 *count = scorecount;
354 *scores = ret;
355 return 0;
356}
357
b23b5a5b 358static int
873fbd0b 359score_compare (const void *a, const void *b)
2f1de3dd
CW
360{
361 const struct score_entry *sa = (const struct score_entry *) a;
362 const struct score_entry *sb = (const struct score_entry *) b;
363 return (sb->score > sa->score) - (sb->score < sa->score);
364}
365
b23b5a5b 366static int
873fbd0b 367score_compare_reverse (const void *a, const void *b)
2f1de3dd
CW
368{
369 const struct score_entry *sa = (const struct score_entry *) a;
370 const struct score_entry *sb = (const struct score_entry *) b;
371 return (sa->score > sb->score) - (sa->score < sb->score);
372}
373
374int
873fbd0b 375push_score (struct score_entry **scores, int *count, int newscore, char *username, char *newdata)
2f1de3dd 376{
a5d107f3 377 struct score_entry *newscores
00a7e408
RS
378 = (struct score_entry *) realloc (*scores,
379 sizeof (struct score_entry) * ((*count) + 1));
2f1de3dd
CW
380 if (!newscores)
381 return -1;
382 newscores[*count].score = newscore;
cd553ffb 383 newscores[*count].username = username;
2f1de3dd
CW
384 newscores[*count].data = newdata;
385 (*count) += 1;
386 *scores = newscores;
387 return 0;
388}
177c0ea7 389
b23b5a5b 390static void
873fbd0b 391sort_scores (struct score_entry *scores, int count, int reverse)
2f1de3dd 392{
a5d107f3 393 qsort (scores, count, sizeof (struct score_entry),
2f1de3dd
CW
394 reverse ? score_compare_reverse : score_compare);
395}
396
b23b5a5b 397static int
873fbd0b 398write_scores (const char *filename, const struct score_entry *scores, int count)
2f1de3dd 399{
177c0ea7 400 FILE *f;
2f1de3dd 401 int i;
a5d107f3 402 char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1);
2f1de3dd
CW
403 if (!tempfile)
404 return -1;
a5d107f3
RS
405 strcpy (tempfile, filename);
406 strcat (tempfile, ".tempXXXXXX");
8eec804f 407#ifdef HAVE_MKSTEMP
a5d107f3 408 if (mkstemp (tempfile) < 0
8eec804f 409#else
a5d107f3 410 if (mktemp (tempfile) != tempfile
8eec804f 411#endif
a5d107f3 412 || !(f = fopen (tempfile, "w")))
2f1de3dd
CW
413 return -1;
414 for (i = 0; i < count; i++)
a5d107f3 415 if (fprintf (f, "%ld %s %s\n", scores[i].score, scores[i].username,
cd553ffb 416 scores[i].data) < 0)
2f1de3dd 417 return -1;
a5d107f3
RS
418 fclose (f);
419 if (rename (tempfile, filename) < 0)
2f1de3dd 420 return -1;
a5d107f3 421 if (chmod (filename, 0644) < 0)
2f1de3dd 422 return -1;
8eec804f 423 return 0;
2f1de3dd 424}
177c0ea7 425
b23b5a5b 426static int
873fbd0b 427lock_file (const char *filename, void **state)
2f1de3dd
CW
428{
429 int fd;
7c4f6873 430 struct stat buf;
2f1de3dd 431 int attempts = 0;
988e88ab 432 const char *lockext = ".lockfile";
a5d107f3 433 char *lockpath = malloc (strlen (filename) + strlen (lockext) + 60);
2f1de3dd
CW
434 if (!lockpath)
435 return -1;
a5d107f3
RS
436 strcpy (lockpath, filename);
437 strcat (lockpath, lockext);
2f1de3dd
CW
438 *state = lockpath;
439 trylock:
440 attempts++;
7c4f6873 441 /* If the lock is over an hour old, delete it. */
a5d107f3
RS
442 if (stat (lockpath, &buf) == 0
443 && (difftime (buf.st_ctime, time (NULL) > 60*60)))
444 unlink (lockpath);
00a7e408
RS
445 fd = open (lockpath, O_CREAT | O_EXCL, 0600);
446 if (fd < 0)
2f1de3dd
CW
447 {
448 if (errno == EEXIST)
449 {
450 /* Break the lock; we won't corrupt the file, but we might
451 lose some scores. */
452 if (attempts > MAX_ATTEMPTS)
7c4f6873 453 {
a5d107f3 454 unlink (lockpath);
7c4f6873
CW
455 attempts = 0;
456 }
a5d107f3 457 sleep ((rand () % 2)+1);
2f1de3dd
CW
458 goto trylock;
459 }
460 else
461 return -1;
462 }
a5d107f3 463 close (fd);
2f1de3dd
CW
464 return 0;
465}
177c0ea7 466
b23b5a5b 467static int
873fbd0b 468unlock_file (const char *filename, void *state)
2f1de3dd
CW
469{
470 char *lockpath = (char *) state;
a5d107f3 471 int ret = unlink (lockpath);
2f1de3dd 472 int saved_errno = errno;
a5d107f3 473 free (lockpath);
2f1de3dd
CW
474 errno = saved_errno;
475 return ret;
476}
ab5796a9 477
65396510
TTN
478
479/* update-game-score.c ends here */