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