1 /* update-game-score.c --- Update a score file
3 Copyright (C) 2002-2011 Free Software Foundation, Inc.
5 Author: Colin Walters <walters@debian.org>
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 /* This program allows a game to securely and atomically update a
24 score file. It should be installed setuid, owned by an appropriate
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).
49 /* Needed for SunOS4, for instance. */
51 extern int optind
, opterr
;
53 static int usage (int err
) NO_RETURN
;
55 #define MAX_ATTEMPTS 5
56 #define MAX_SCORES 200
57 #define MAX_DATA_LEN 1024
60 /* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
61 #define difftime(t1, t0) (double)((t1) - (t0))
67 fprintf (stdout
, "Usage: update-game-score [-m MAX] [-r] [-d DIR] game/scorefile SCORE DATA\n");
68 fprintf (stdout
, " update-game-score -h\n");
69 fprintf (stdout
, " -h\t\tDisplay this help.\n");
70 fprintf (stdout
, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
71 fprintf (stdout
, " -r\t\tSort the scores in increasing order.\n");
72 fprintf (stdout
, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
76 static int lock_file (const char *filename
, void **state
);
77 static int unlock_file (const char *filename
, void *state
);
86 static int read_scores (const char *filename
, struct score_entry
**scores
,
88 static int push_score (struct score_entry
**scores
, int *count
,
89 int newscore
, char *username
, char *newdata
);
90 static void sort_scores (struct score_entry
*scores
, int count
, int reverse
);
91 static int write_scores (const char *filename
,
92 const struct score_entry
*scores
, int count
);
94 static void lose (const char *msg
) NO_RETURN
;
97 lose (const char *msg
)
99 fprintf (stderr
, "%s\n", msg
);
103 static void lose_syserr (const char *msg
) NO_RETURN
;
105 /* Taken from sysdep.c. */
106 #ifndef HAVE_STRERROR
109 strerror (int errnum
)
111 extern char *sys_errlist
[];
114 if (errnum
>= 0 && errnum
< sys_nerr
)
115 return sys_errlist
[errnum
];
116 return (char *) "Unknown error";
118 #endif /* not WINDOWSNT */
119 #endif /* ! HAVE_STRERROR */
122 lose_syserr (const char *msg
)
124 fprintf (stderr
, "%s: %s\n", msg
, strerror (errno
));
132 struct passwd
*buf
= getpwuid (getuid ());
136 int uid
= (int) getuid ();
140 name
= malloc (count
+1);
143 sprintf (name
, "%d", uid
);
150 get_prefix (int running_suid
, const char *user_prefix
)
152 if (!running_suid
&& user_prefix
== NULL
)
153 lose ("Not using a shared game directory, and no prefix given.");
156 #ifdef HAVE_SHARED_GAME_DIR
157 return HAVE_SHARED_GAME_DIR
;
159 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
166 main (int argc
, char **argv
)
170 char *user_id
, *scorefile
;
171 const char *prefix
, *user_prefix
= NULL
;
173 struct score_entry
*scores
;
174 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
179 while ((c
= getopt (argc
, argv
, "hrm:d:")) != -1)
183 usage (EXIT_SUCCESS
);
186 user_prefix
= optarg
;
193 if (max
> MAX_SCORES
)
197 usage (EXIT_FAILURE
);
200 if (optind
+3 != argc
)
201 usage (EXIT_FAILURE
);
203 running_suid
= (getuid () != geteuid ());
205 prefix
= get_prefix (running_suid
, user_prefix
);
207 scorefile
= malloc (strlen (prefix
) + strlen (argv
[optind
]) + 2);
209 lose_syserr ("Couldn't allocate score file");
211 strcpy (scorefile
, prefix
);
212 strcat (scorefile
, "/");
213 strcat (scorefile
, argv
[optind
]);
214 newscore
= atoi (argv
[optind
+1]);
215 newdata
= argv
[optind
+2];
216 if (strlen (newdata
) > MAX_DATA_LEN
)
217 newdata
[MAX_DATA_LEN
] = '\0';
219 user_id
= get_user_id ();
221 lose_syserr ("Couldn't determine user id");
223 if (stat (scorefile
, &buf
) < 0)
224 lose_syserr ("Failed to access scores file");
226 if (lock_file (scorefile
, &lockstate
) < 0)
227 lose_syserr ("Failed to lock scores file");
229 if (read_scores (scorefile
, &scores
, &scorecount
) < 0)
231 unlock_file (scorefile
, lockstate
);
232 lose_syserr ("Failed to read scores file");
234 push_score (&scores
, &scorecount
, newscore
, user_id
, newdata
);
235 sort_scores (scores
, scorecount
, reverse
);
236 /* Limit the number of scores. If we're using reverse sorting, then
237 also increment the beginning of the array, to skip over the
238 *smallest* scores. Otherwise, just decrementing the number of
239 scores suffices, since the smallest is at the end. */
240 if (scorecount
> MAX_SCORES
)
243 scores
+= (scorecount
- MAX_SCORES
);
244 scorecount
= MAX_SCORES
;
246 if (write_scores (scorefile
, scores
, scorecount
) < 0)
248 unlock_file (scorefile
, lockstate
);
249 lose_syserr ("Failed to write scores file");
251 unlock_file (scorefile
, lockstate
);
256 read_score (FILE *f
, struct score_entry
*score
)
261 while ((c
= getc (f
)) != EOF
265 score
->score
+= (c
-48);
267 while ((c
= getc (f
)) != EOF
276 if (getdelim (&score
->username
, &count
, ' ', f
) < 1
277 || score
->username
== NULL
)
280 score
->username
[strlen (score
->username
)-1] = '\0';
286 char *username
= malloc (unamelen
);
290 while ((c
= getc (f
)) != EOF
293 if (unameread
>= unamelen
-1)
294 if (!(username
= realloc (username
, unamelen
*= 2)))
296 username
[unameread
] = c
;
301 username
[unameread
] = '\0';
302 score
->username
= username
;
310 if (getline (&score
->data
, &len
, f
) < 0)
312 score
->data
[strlen (score
->data
)-1] = '\0';
318 char *buf
= malloc (len
);
321 while ((c
= getc (f
)) != EOF
326 if (!(buf
= realloc (buf
, len
*= 2)))
333 score
->data
[cur
] = '\0';
340 read_scores (const char *filename
, struct score_entry
**scores
, int *count
)
342 int readval
, scorecount
, cursize
;
343 struct score_entry
*ret
;
344 FILE *f
= fopen (filename
, "r");
349 ret
= (struct score_entry
*) malloc (sizeof (struct score_entry
) * cursize
);
352 while ((readval
= read_score (f
, &ret
[scorecount
])) == 0)
354 /* We encoutered an error */
358 if (scorecount
>= cursize
)
361 ret
= (struct score_entry
*)
362 realloc (ret
, (sizeof (struct score_entry
) * cursize
));
373 score_compare (const void *a
, const void *b
)
375 const struct score_entry
*sa
= (const struct score_entry
*) a
;
376 const struct score_entry
*sb
= (const struct score_entry
*) b
;
377 return (sb
->score
> sa
->score
) - (sb
->score
< sa
->score
);
381 score_compare_reverse (const void *a
, const void *b
)
383 const struct score_entry
*sa
= (const struct score_entry
*) a
;
384 const struct score_entry
*sb
= (const struct score_entry
*) b
;
385 return (sa
->score
> sb
->score
) - (sa
->score
< sb
->score
);
389 push_score (struct score_entry
**scores
, int *count
, int newscore
, char *username
, char *newdata
)
391 struct score_entry
*newscores
392 = (struct score_entry
*) realloc (*scores
,
393 sizeof (struct score_entry
) * ((*count
) + 1));
396 newscores
[*count
].score
= newscore
;
397 newscores
[*count
].username
= username
;
398 newscores
[*count
].data
= newdata
;
405 sort_scores (struct score_entry
*scores
, int count
, int reverse
)
407 qsort (scores
, count
, sizeof (struct score_entry
),
408 reverse
? score_compare_reverse
: score_compare
);
412 write_scores (const char *filename
, const struct score_entry
*scores
, int count
)
416 char *tempfile
= malloc (strlen (filename
) + strlen (".tempXXXXXX") + 1);
419 strcpy (tempfile
, filename
);
420 strcat (tempfile
, ".tempXXXXXX");
422 if (mkstemp (tempfile
) < 0
424 if (mktemp (tempfile
) != tempfile
426 || !(f
= fopen (tempfile
, "w")))
428 for (i
= 0; i
< count
; i
++)
429 if (fprintf (f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
433 if (rename (tempfile
, filename
) < 0)
435 if (chmod (filename
, 0644) < 0)
441 lock_file (const char *filename
, void **state
)
446 const char *lockext
= ".lockfile";
447 char *lockpath
= malloc (strlen (filename
) + strlen (lockext
) + 60);
450 strcpy (lockpath
, filename
);
451 strcat (lockpath
, lockext
);
455 /* If the lock is over an hour old, delete it. */
456 if (stat (lockpath
, &buf
) == 0
457 && (difftime (buf
.st_ctime
, time (NULL
) > 60*60)))
459 fd
= open (lockpath
, O_CREAT
| O_EXCL
, 0600);
464 /* Break the lock; we won't corrupt the file, but we might
466 if (attempts
> MAX_ATTEMPTS
)
471 sleep ((rand () % 2)+1);
482 unlock_file (const char *filename
, void *state
)
484 char *lockpath
= (char *) state
;
485 int ret
= unlink (lockpath
);
486 int saved_errno
= errno
;
493 /* update-game-score.c ends here */