From: Joshua Kwan Date: Sun, 4 Jan 2004 00:59:48 +0000 (+0000) Subject: Add strlcpy.c back and fix some locking issues (replace all with fcntl) X-Git-Tag: v1.6.1-roc-dev~639 X-Git-Url: https://skyeroc.xyz/gitweb/?a=commitdiff_plain;h=2a2f13d3b5b462be1b77c4c01d57dd469d1701d4;p=dgamelaunch Add strlcpy.c back and fix some locking issues (replace all with fcntl) git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@59 db0b04b0-f4d1-0310-9a6d-de3e77497b0e --- diff --git a/dgamelaunch.c b/dgamelaunch.c index 890f1d9..749611a 100644 --- a/dgamelaunch.c +++ b/dgamelaunch.c @@ -140,12 +140,18 @@ gen_inprogress_lock () { char lockfile[130]; int fd; + struct flock fl = { 0 }; + + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; snprintf (lockfile, 130, "%s%s:%s", LOC_INPROGRESSDIR, me->username, ttyrec_filename); fd = open (lockfile, O_WRONLY | O_CREAT, 0644); - if (flock (fd, LOCK_EX)) + if (fcntl (fd, F_SETLKW, &fl) == -1) exit (68); } @@ -263,6 +269,12 @@ populate_games (int *l) char fullname[130], ttyrecname[130]; char *replacestr; struct dg_game **games = NULL; + struct flock fl = { 0 }; + + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; len = 0; @@ -271,11 +283,16 @@ populate_games (int *l) while ((pdirent = readdir (pdir))) { + if (!strcmp (pdirent->d_name, ".") || !strcmp(pdirent->d_name, "..")) + continue; + snprintf (fullname, 130, "%s%s", LOC_INPROGRESSDIR, pdirent->d_name); fd = 0; - fd = open (fullname, O_RDONLY); - if ((fd > 0) && flock (fd, LOCK_EX | LOCK_NB)) + /* O_RDWR here should be O_RDONLY, but we need to test for + * an exclusive lock */ + fd = open (fullname, O_RDWR); + if ((fd > 0) && fcntl(fd, F_SETLK, &fl) == -1) { /* stat to check idle status */ @@ -316,7 +333,9 @@ populate_games (int *l) /* clean dead ones */ unlink (fullname); } - flock (fd, LOCK_UN | LOCK_NB); + fl.l_type = F_UNLCK; + + fcntl (fd, F_UNLCK, &fl); close (fd); } @@ -832,6 +851,12 @@ readfile (int nolock) { FILE *fp = NULL, *fpl = NULL; char buf[1200]; + struct flock fl; + + fl.l_type = F_RDLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; memset (buf, 1024, 0); @@ -842,7 +867,7 @@ readfile (int nolock) fpl = fopen ("/dgl-lock", "r"); if (!fpl) exit (106); - if (flock (fileno (fpl), LOCK_SH)) + if (fcntl (fileno(fpl), F_SETLKW, &fl) == -1) exit (114); } diff --git a/strlcpy.c b/strlcpy.c new file mode 100644 index 0000000..51e8670 --- /dev/null +++ b/strlcpy.c @@ -0,0 +1,74 @@ +/* $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = + "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy (dst, src, siz) + char *dst; + const char *src; + size_t siz; +{ + register char *d = dst; + register const char *s = src; + register size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) + { + do + { + if ((*d++ = *s++) == 0) + break; + } + while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) + { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return (s - src - 1); /* count does not include NUL */ +}