]>
code.delx.au - gnu-emacs/blob - lib-src/update-game-score.c
1 /* update-game-score.c --- Update a score file
3 Copyright (C) 2002-2012 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).
51 #define MAX_ATTEMPTS 5
52 #define MAX_SCORES 200
53 #define MAX_DATA_LEN 1024
56 /* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
57 #define difftime(t1, t0) (double)((t1) - (t0))
63 fprintf (stdout
, "Usage: update-game-score [-m MAX] [-r] [-d DIR] game/scorefile SCORE DATA\n");
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");
72 static int lock_file (const char *filename
, void **state
);
73 static int unlock_file (const char *filename
, void *state
);
82 static int read_scores (const char *filename
, struct score_entry
**scores
,
84 static int push_score (struct score_entry
**scores
, int *count
,
85 int newscore
, char *username
, char *newdata
);
86 static void sort_scores (struct score_entry
*scores
, int count
, int reverse
);
87 static int write_scores (const char *filename
,
88 const struct score_entry
*scores
, int count
);
91 lose (const char *msg
)
93 fprintf (stderr
, "%s\n", msg
);
97 /* Taken from sysdep.c. */
101 strerror (int errnum
)
103 extern char *sys_errlist
[];
106 if (errnum
>= 0 && errnum
< sys_nerr
)
107 return sys_errlist
[errnum
];
108 return (char *) "Unknown error";
110 #endif /* not WINDOWSNT */
111 #endif /* ! HAVE_STRERROR */
113 static _Noreturn
void
114 lose_syserr (const char *msg
)
116 fprintf (stderr
, "%s: %s\n", msg
, strerror (errno
));
123 struct passwd
*buf
= getpwuid (getuid ());
126 long uid
= getuid ();
127 char *name
= malloc (sizeof uid
* CHAR_BIT
/ 3 + 1);
129 sprintf (name
, "%ld", uid
);
136 get_prefix (int running_suid
, const char *user_prefix
)
138 if (!running_suid
&& user_prefix
== NULL
)
139 lose ("Not using a shared game directory, and no prefix given.");
142 #ifdef HAVE_SHARED_GAME_DIR
143 return HAVE_SHARED_GAME_DIR
;
145 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
152 main (int argc
, char **argv
)
156 char *user_id
, *scorefile
;
157 const char *prefix
, *user_prefix
= NULL
;
159 struct score_entry
*scores
;
160 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
165 while ((c
= getopt (argc
, argv
, "hrm:d:")) != -1)
169 usage (EXIT_SUCCESS
);
172 user_prefix
= optarg
;
179 if (max
> MAX_SCORES
)
183 usage (EXIT_FAILURE
);
186 if (optind
+3 != argc
)
187 usage (EXIT_FAILURE
);
189 running_suid
= (getuid () != geteuid ());
191 prefix
= get_prefix (running_suid
, user_prefix
);
193 scorefile
= malloc (strlen (prefix
) + strlen (argv
[optind
]) + 2);
195 lose_syserr ("Couldn't allocate score file");
197 strcpy (scorefile
, prefix
);
198 strcat (scorefile
, "/");
199 strcat (scorefile
, argv
[optind
]);
200 newscore
= atoi (argv
[optind
+1]);
201 newdata
= argv
[optind
+2];
202 if (strlen (newdata
) > MAX_DATA_LEN
)
203 newdata
[MAX_DATA_LEN
] = '\0';
205 user_id
= get_user_id ();
207 lose_syserr ("Couldn't determine user id");
209 if (stat (scorefile
, &buf
) < 0)
210 lose_syserr ("Failed to access scores file");
212 if (lock_file (scorefile
, &lockstate
) < 0)
213 lose_syserr ("Failed to lock scores file");
215 if (read_scores (scorefile
, &scores
, &scorecount
) < 0)
217 unlock_file (scorefile
, lockstate
);
218 lose_syserr ("Failed to read scores file");
220 push_score (&scores
, &scorecount
, newscore
, user_id
, newdata
);
221 sort_scores (scores
, scorecount
, reverse
);
222 /* Limit the number of scores. If we're using reverse sorting, then
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. */
226 if (scorecount
> MAX_SCORES
)
229 scores
+= (scorecount
- MAX_SCORES
);
230 scorecount
= MAX_SCORES
;
232 if (write_scores (scorefile
, scores
, scorecount
) < 0)
234 unlock_file (scorefile
, lockstate
);
235 lose_syserr ("Failed to write scores file");
237 unlock_file (scorefile
, lockstate
);
242 read_score (FILE *f
, struct score_entry
*score
)
247 while ((c
= getc (f
)) != EOF
251 score
->score
+= (c
-48);
253 while ((c
= getc (f
)) != EOF
262 if (getdelim (&score
->username
, &count
, ' ', f
) < 1
263 || score
->username
== NULL
)
266 score
->username
[strlen (score
->username
)-1] = '\0';
272 char *username
= malloc (unamelen
);
276 while ((c
= getc (f
)) != EOF
279 if (unameread
>= unamelen
-1)
280 if (!(username
= realloc (username
, unamelen
*= 2)))
282 username
[unameread
] = c
;
287 username
[unameread
] = '\0';
288 score
->username
= username
;
296 if (getline (&score
->data
, &len
, f
) < 0)
298 score
->data
[strlen (score
->data
)-1] = '\0';
304 char *buf
= malloc (len
);
307 while ((c
= getc (f
)) != EOF
312 if (!(buf
= realloc (buf
, len
*= 2)))
319 score
->data
[cur
] = '\0';
326 read_scores (const char *filename
, struct score_entry
**scores
, int *count
)
328 int readval
, scorecount
, cursize
;
329 struct score_entry
*ret
;
330 FILE *f
= fopen (filename
, "r");
335 ret
= (struct score_entry
*) malloc (sizeof (struct score_entry
) * cursize
);
338 while ((readval
= read_score (f
, &ret
[scorecount
])) == 0)
340 /* We encountered an error. */
344 if (scorecount
>= cursize
)
347 ret
= (struct score_entry
*)
348 realloc (ret
, (sizeof (struct score_entry
) * cursize
));
359 score_compare (const void *a
, const void *b
)
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
);
367 score_compare_reverse (const void *a
, const void *b
)
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
);
375 push_score (struct score_entry
**scores
, int *count
, int newscore
, char *username
, char *newdata
)
377 struct score_entry
*newscores
378 = (struct score_entry
*) realloc (*scores
,
379 sizeof (struct score_entry
) * ((*count
) + 1));
382 newscores
[*count
].score
= newscore
;
383 newscores
[*count
].username
= username
;
384 newscores
[*count
].data
= newdata
;
391 sort_scores (struct score_entry
*scores
, int count
, int reverse
)
393 qsort (scores
, count
, sizeof (struct score_entry
),
394 reverse
? score_compare_reverse
: score_compare
);
398 write_scores (const char *filename
, const struct score_entry
*scores
, int count
)
402 char *tempfile
= malloc (strlen (filename
) + strlen (".tempXXXXXX") + 1);
405 strcpy (tempfile
, filename
);
406 strcat (tempfile
, ".tempXXXXXX");
408 if (mkstemp (tempfile
) < 0
410 if (mktemp (tempfile
) != tempfile
412 || !(f
= fopen (tempfile
, "w")))
414 for (i
= 0; i
< count
; i
++)
415 if (fprintf (f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
419 if (rename (tempfile
, filename
) < 0)
421 if (chmod (filename
, 0644) < 0)
427 lock_file (const char *filename
, void **state
)
432 const char *lockext
= ".lockfile";
433 char *lockpath
= malloc (strlen (filename
) + strlen (lockext
) + 60);
436 strcpy (lockpath
, filename
);
437 strcat (lockpath
, lockext
);
441 /* If the lock is over an hour old, delete it. */
442 if (stat (lockpath
, &buf
) == 0
443 && (difftime (buf
.st_ctime
, time (NULL
) > 60*60)))
445 fd
= open (lockpath
, O_CREAT
| O_EXCL
, 0600);
450 /* Break the lock; we won't corrupt the file, but we might
452 if (attempts
> MAX_ATTEMPTS
)
457 sleep ((rand () % 2)+1);
468 unlock_file (const char *filename
, void *state
)
470 char *lockpath
= (char *) state
;
471 int ret
= unlink (lockpath
);
472 int saved_errno
= errno
;
479 /* update-game-score.c ends here */