1 /* update-game-score.c --- Update a score file
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Author: Colin Walters <walters@debian.org>
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
24 /* This program allows a game to securely and atomically update a
25 score file. It should be installed setuid, owned by an appropriate
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).
59 /* Needed for SunOS4, for instance. */
61 extern int optind
, opterr
;
63 #define MAX_ATTEMPTS 5
64 #define MAX_SCORES 200
65 #define MAX_DATA_LEN 1024
67 /* Declare the prototype for a general external function. */
68 #if defined (PROTOTYPES) || defined (WINDOWSNT)
69 #define P_(proto) proto
75 /* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
76 #define difftime(t1, t0) (double)((t1) - (t0))
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");
92 int lock_file
P_ ((const char *filename
, void **state
));
93 int unlock_file
P_ ((const char *filename
, void *state
));
102 int read_scores
P_ ((const char *filename
, struct score_entry
**scores
,
104 int push_score
P_ ((struct score_entry
**scores
, int *count
,
105 int newscore
, char *username
, char *newdata
));
106 void sort_scores
P_ ((struct score_entry
*scores
, int count
, int reverse
));
107 int write_scores
P_ ((const char *filename
, const struct score_entry
*scores
,
110 void lose
P_ ((const char *msg
)) NO_RETURN
;
116 fprintf (stderr
, "%s\n", msg
);
120 void lose_syserr
P_ ((const char *msg
)) NO_RETURN
;
122 /* Taken from sysdep.c. */
123 #ifndef HAVE_STRERROR
129 extern char *sys_errlist
[];
132 if (errnum
>= 0 && errnum
< sys_nerr
)
133 return sys_errlist
[errnum
];
134 return (char *) "Unknown error";
136 #endif /* not WINDOWSNT */
137 #endif /* ! HAVE_STRERROR */
143 fprintf (stderr
, "%s: %s\n", msg
, strerror (errno
));
148 get_user_id
P_ ((void))
151 struct passwd
*buf
= getpwuid (getuid ());
155 int uid
= (int) getuid ();
159 name
= malloc (count
+1);
162 sprintf (name
, "%d", uid
);
169 get_prefix (running_suid
, user_prefix
)
173 if (!running_suid
&& user_prefix
== NULL
)
174 lose ("Not using a shared game directory, and no prefix given.");
177 #ifdef HAVE_SHARED_GAME_DIR
178 return HAVE_SHARED_GAME_DIR
;
180 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
193 char *user_id
, *scorefile
, *prefix
, *user_prefix
= NULL
;
195 struct score_entry
*scores
;
196 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
201 while ((c
= getopt (argc
, argv
, "hrm:d:")) != -1)
205 usage (EXIT_SUCCESS
);
208 user_prefix
= optarg
;
215 if (max
> MAX_SCORES
)
219 usage (EXIT_FAILURE
);
222 if (optind
+3 != argc
)
223 usage (EXIT_FAILURE
);
225 running_suid
= (getuid () != geteuid ());
227 prefix
= get_prefix (running_suid
, user_prefix
);
229 scorefile
= malloc (strlen (prefix
) + strlen (argv
[optind
]) + 2);
231 lose_syserr ("Couldn't allocate score file");
233 strcpy (scorefile
, prefix
);
234 strcat (scorefile
, "/");
235 strcat (scorefile
, argv
[optind
]);
236 newscore
= atoi (argv
[optind
+1]);
237 newdata
= argv
[optind
+2];
238 if (strlen (newdata
) > MAX_DATA_LEN
)
239 newdata
[MAX_DATA_LEN
] = '\0';
241 user_id
= get_user_id ();
243 lose_syserr ("Couldn't determine user id");
245 if (stat (scorefile
, &buf
) < 0)
246 lose_syserr ("Failed to access scores file");
248 if (lock_file (scorefile
, &lockstate
) < 0)
249 lose_syserr ("Failed to lock scores file");
251 if (read_scores (scorefile
, &scores
, &scorecount
) < 0)
253 unlock_file (scorefile
, lockstate
);
254 lose_syserr ("Failed to read scores file");
256 push_score (&scores
, &scorecount
, newscore
, user_id
, newdata
);
257 sort_scores (scores
, scorecount
, reverse
);
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
);
265 scores
+= (scorecount
- MAX_SCORES
);
266 if (write_scores (scorefile
, scores
, scorecount
) < 0)
268 unlock_file (scorefile
, lockstate
);
269 lose_syserr ("Failed to write scores file");
271 unlock_file (scorefile
, lockstate
);
276 read_score (f
, score
)
278 struct score_entry
*score
;
283 while ((c
= getc (f
)) != EOF
287 score
->score
+= (c
-48);
289 while ((c
= getc (f
)) != EOF
298 if (getdelim (&score
->username
, &count
, ' ', f
) < 1
299 || score
->username
== NULL
)
302 score
->username
[strlen (score
->username
)-1] = '\0';
308 char *username
= malloc (unamelen
);
312 while ((c
= getc (f
)) != EOF
315 if (unameread
>= unamelen
-1)
316 if (!(username
= realloc (username
, unamelen
*= 2)))
318 username
[unameread
] = c
;
323 username
[unameread
] = '\0';
324 score
->username
= username
;
332 if (getline (&score
->data
, &len
, f
) < 0)
334 score
->data
[strlen (score
->data
)-1] = '\0';
340 char *buf
= malloc (len
);
343 while ((c
= getc (f
)) != EOF
348 if (!(buf
= realloc (buf
, len
*= 2)))
355 score
->data
[cur
] = '\0';
362 read_scores (filename
, scores
, count
)
363 const char *filename
;
364 struct score_entry
**scores
;
367 int readval
, scorecount
, cursize
;
368 struct score_entry
*ret
;
369 FILE *f
= fopen (filename
, "r");
374 ret
= (struct score_entry
*) malloc (sizeof (struct score_entry
) * cursize
);
377 while ((readval
= read_score (f
, &ret
[scorecount
])) == 0)
379 /* We encoutered an error */
383 if (scorecount
>= cursize
)
386 ret
= (struct score_entry
*)
387 realloc (ret
, (sizeof (struct score_entry
) * cursize
));
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
);
408 score_compare_reverse (a
, b
)
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
);
418 push_score (scores
, count
, newscore
, username
, newdata
)
419 struct score_entry
**scores
;
420 int *count
; int newscore
;
424 struct score_entry
*newscores
425 = (struct score_entry
*) realloc (*scores
,
426 sizeof (struct score_entry
) * ((*count
) + 1));
429 newscores
[*count
].score
= newscore
;
430 newscores
[*count
].username
= username
;
431 newscores
[*count
].data
= newdata
;
438 sort_scores (scores
, count
, reverse
)
439 struct score_entry
*scores
;
443 qsort (scores
, count
, sizeof (struct score_entry
),
444 reverse
? score_compare_reverse
: score_compare
);
448 write_scores (filename
, scores
, count
)
449 const char *filename
;
450 const struct score_entry
* scores
;
455 char *tempfile
= malloc (strlen (filename
) + strlen (".tempXXXXXX") + 1);
458 strcpy (tempfile
, filename
);
459 strcat (tempfile
, ".tempXXXXXX");
461 if (mkstemp (tempfile
) < 0
463 if (mktemp (tempfile
) != tempfile
465 || !(f
= fopen (tempfile
, "w")))
467 for (i
= 0; i
< count
; i
++)
468 if (fprintf (f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
472 if (rename (tempfile
, filename
) < 0)
474 if (chmod (filename
, 0644) < 0)
480 lock_file (filename
, state
)
481 const char *filename
;
487 char *lockext
= ".lockfile";
488 char *lockpath
= malloc (strlen (filename
) + strlen (lockext
) + 60);
491 strcpy (lockpath
, filename
);
492 strcat (lockpath
, lockext
);
496 /* If the lock is over an hour old, delete it. */
497 if (stat (lockpath
, &buf
) == 0
498 && (difftime (buf
.st_ctime
, time (NULL
) > 60*60)))
500 fd
= open (lockpath
, O_CREAT
| O_EXCL
, 0600);
505 /* Break the lock; we won't corrupt the file, but we might
507 if (attempts
> MAX_ATTEMPTS
)
512 sleep ((rand () % 2)+1);
523 unlock_file (filename
, state
)
524 const char *filename
;
527 char *lockpath
= (char *) state
;
528 int ret
= unlink (lockpath
);
529 int saved_errno
= errno
;
535 /* arch-tag: 2bf5c52e-4beb-463a-954e-c58b9c64736b
536 (do not change this comment) */
538 /* update-game-score.c ends here */