[BACK]Return to t_lockf.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / tests / kernel

Annotation of src/tests/kernel/t_lockf.c, Revision 1.9

1.9     ! christos    1: /*     $NetBSD: t_lockf.c,v 1.8 2013/02/20 02:22:48 pgoyette Exp $     */
1.1       pgoyette    2:
                      3: /*-
                      4:  * Copyright (c) 2000 The NetBSD Foundation, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     17:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     18:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     19:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     20:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     21:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     22:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     23:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     24:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     25:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     26:  * POSSIBILITY OF SUCH DAMAGE.
                     27:  */
                     28:
                     29: #include <atf-c.h>
                     30: #include <err.h>
                     31: #include <errno.h>
                     32: #include <fcntl.h>
                     33: #include <signal.h>
                     34: #include <stdio.h>
                     35: #include <stdlib.h>
                     36: #include <string.h>
                     37: #include <unistd.h>
                     38:
                     39: #include <sys/types.h>
                     40: #include <sys/wait.h>
                     41: #include <sys/ptrace.h>
                     42:
                     43: /*
                     44:  * lockf1 regression test:
                     45:  *
                     46:  * Tests:
                     47:  * Fork N child processes, each of which gets M random byte range locks
                     48:  * on a common file.  We ignore all lock errors (practically speaking,
                     49:  * this means EDEADLK or ENOLOCK), but we make numerous passes over all
                     50:  * the children to make sure that they are still awake.  (We do this by
                     51:  * verifying that we can ptrace(ATTACH/DETACH) to the children and get
                     52:  * their status via waitpid().)
                     53:  * When finished, reap all the children.
                     54:  */
                     55:
1.5       pgoyette   56: #define        nlocks          500     /* number of locks per thread */
                     57: #define        nprocs          10      /* number of processes to spawn */
                     58: #define        npasses         50      /* number of passes to make over the children */
                     59: #define        sleeptime       150000  /* sleep time between locks, usec */
                     60: #define        filesize        8192    /* size of file to lock */
1.1       pgoyette   61:
                     62: const char *lockfile = "lockf_test";
                     63:
                     64: static u_int32_t
                     65: random_uint32(void)
                     66: {
                     67:        return lrand48();
                     68: }
                     69:
                     70: static void
                     71: trylocks(int id)
                     72: {
1.9     ! christos   73:        int i, fd;
1.1       pgoyette   74:
                     75:        srand48(getpid());
                     76:
                     77:        fd = open (lockfile, O_RDWR, 0);
                     78:
                     79:        if (fd < 0)
                     80:                err(1, "%s", lockfile);
                     81:
                     82:        printf("%d: start\n", id);
                     83:
1.5       pgoyette   84:        for (i = 0; i < nlocks; i++) {
1.1       pgoyette   85:                struct flock fl;
                     86:
1.5       pgoyette   87:                fl.l_start = random_uint32() % filesize;
                     88:                fl.l_len = random_uint32() % filesize;
1.1       pgoyette   89:                switch (random_uint32() % 3) {
                     90:                case 0:
                     91:                        fl.l_type = F_RDLCK;
                     92:                        break;
                     93:                case 1:
                     94:                        fl.l_type = F_WRLCK;
                     95:                        break;
                     96:                case 2:
                     97:                        fl.l_type = F_UNLCK;
                     98:                        break;
                     99:                }
                    100:                fl.l_whence = SEEK_SET;
                    101:
1.9     ! christos  102:                (void)fcntl(fd, F_SETLKW, &fl);
1.1       pgoyette  103:
                    104:                if (usleep(sleeptime) < 0)
                    105:                  err(1, "usleep");
                    106:        }
                    107:        printf("%d: done\n", id);
                    108:        close (fd);
                    109: }
                    110:
                    111: ATF_TC(randlock);
                    112: ATF_TC_HEAD(randlock, tc)
                    113: {
                    114:
                    115:        atf_tc_set_md_var(tc, "timeout", "300");
                    116:        atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) locking");
                    117: }
                    118:
                    119: ATF_TC_BODY(randlock, tc)
                    120: {
                    121:        int i, j, fd;
1.6       pgoyette  122:        int pipe_fd[2];
1.1       pgoyette  123:        pid_t *pid;
                    124:        int status;
1.6       pgoyette  125:        char pipe_in, pipe_out;
1.8       pgoyette  126:        const char pipe_errmsg[] = "child: pipe write failed\n";
1.1       pgoyette  127:
                    128:        (void)unlink(lockfile);
                    129:
                    130:        fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
                    131:        ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
                    132:
1.5       pgoyette  133:        ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
1.1       pgoyette  134:            "ftruncate(%s): %s", lockfile, strerror(errno));
                    135:
1.6       pgoyette  136:        ATF_REQUIRE_MSG(pipe(pipe_fd) == 0, "pipe: %s", strerror(errno));
                    137:
1.1       pgoyette  138:        fsync(fd);
                    139:        close(fd);
                    140:
                    141:        pid = malloc(nprocs * sizeof(pid_t));
                    142:
1.5       pgoyette  143:        for (i = 0; i < nprocs; i++) {
1.6       pgoyette  144:                pipe_out = (char)('A' + i);
1.1       pgoyette  145:                pid[i] = fork();
                    146:                switch (pid[i]) {
                    147:                case 0:
1.6       pgoyette  148:                        if (write(pipe_fd[1], &pipe_out, 1) != 1)
1.7       pgoyette  149:                                write(STDERR_FILENO, pipe_errmsg,
1.8       pgoyette  150:                                    __arraycount(pipe_errmsg) - 1);
1.6       pgoyette  151:                        else
                    152:                                trylocks(i);
1.1       pgoyette  153:                        _exit(0);
                    154:                        break;
                    155:                case -1:
                    156:                        atf_tc_fail("fork %d failed", i);
                    157:                        break;
                    158:                default:
1.6       pgoyette  159:                        ATF_REQUIRE_MSG(read(pipe_fd[0], &pipe_in, 1) == 1,
                    160:                            "parent: read_pipe(%i): %s", i, strerror(errno));
                    161:                        ATF_REQUIRE_MSG(pipe_in == pipe_out,
                    162:                            "parent: pipe does not match");
1.1       pgoyette  163:                        break;
                    164:                }
                    165:        }
1.4       pgoyette  166:        for (j = 0; j < npasses; j++) {
1.1       pgoyette  167:                printf("parent: run %i\n", j+1);
1.5       pgoyette  168:                for (i = 0; i < nprocs; i++) {
1.1       pgoyette  169:                        ATF_REQUIRE_MSG(ptrace(PT_ATTACH, pid[i], 0, 0) >= 0,
                    170:                            "ptrace attach %d", pid[i]);
                    171:                        ATF_REQUIRE_MSG(waitpid(pid[i], &status, WUNTRACED) >= 0,
                    172:                            "waitpid(ptrace)");
1.5       pgoyette  173:                        usleep(sleeptime / 3);
1.1       pgoyette  174:                        ATF_REQUIRE_MSG(ptrace(PT_DETACH, pid[i], (caddr_t)1,
                    175:                                               0) >= 0,
                    176:                            "ptrace detach %d", pid[i]);
1.5       pgoyette  177:                        usleep(sleeptime / 3);
1.1       pgoyette  178:                }
                    179:        }
1.5       pgoyette  180:        for (i = 0; i < nprocs; i++) {
1.1       pgoyette  181:                printf("reap %d: ", i);
                    182:                fflush(stdout);
                    183:                kill(pid[i], SIGINT);
                    184:                waitpid(pid[i], &status, 0);
                    185:                printf(" status %d\n", status);
                    186:        }
                    187:        atf_tc_pass();
                    188: }
                    189:
                    190: static int
                    191: dolock(int fd, int op, off_t lk_off, off_t lk_size)
                    192: {
                    193:        off_t result;
                    194:        int ret;
                    195:
                    196:        result = lseek(fd, lk_off, SEEK_SET);
                    197:        if (result == -1) {
                    198:                return errno;
                    199:        }
                    200:        ATF_REQUIRE_MSG(result == lk_off, "lseek to wrong offset");
                    201:        ret = lockf(fd, op, lk_size);
                    202:        if (ret == -1) {
                    203:                return errno;
                    204:        }
                    205:        return 0;
                    206: }
                    207:
                    208: ATF_TC(deadlock);
                    209: ATF_TC_HEAD(deadlock, tc)
                    210: {
                    211:
                    212:        atf_tc_set_md_var(tc, "timeout", "30");
                    213:        atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) deadlock detection");
                    214: }
                    215:
                    216: ATF_TC_BODY(deadlock, tc)
                    217: {
                    218:        int fd;
                    219:        int error;
                    220:        int ret;
                    221:        pid_t pid;
                    222:
                    223:        (void)unlink(lockfile);
                    224:
                    225:        fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
                    226:        ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
                    227:
1.5       pgoyette  228:        ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
1.1       pgoyette  229:            "ftruncate(%s): %s", lockfile, strerror(errno));
                    230:
                    231:        fsync(fd);
                    232:
                    233:        error = dolock(fd, F_LOCK, 0, 1);
                    234:        ATF_REQUIRE_MSG(error == 0, "initial dolock: %s", strerror(errno));
                    235:
                    236:        pid = fork();
                    237:        ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
                    238:        if (pid == 0) {
                    239:                error = dolock(fd, F_LOCK, 1, 1);
                    240:                ATF_REQUIRE_MSG(error == 0, "child dolock: %s",
                    241:                    strerror(errno));
                    242:                dolock(fd, F_LOCK, 0, 1);       /* will block */
                    243:                atf_tc_fail("child did not block");
                    244:        }
                    245:        sleep(1);       /* give child time to grab its lock then block */
                    246:
                    247:        error = dolock(fd, F_LOCK, 1, 1);
1.2       pgoyette  248:        ATF_REQUIRE_MSG(error == EDEADLK, "parent did not detect deadlock: %s",
1.1       pgoyette  249:            strerror(errno));
                    250:        ret = kill(pid, SIGKILL);
                    251:        ATF_REQUIRE_MSG(ret != -1, "failed to kill child: %s", strerror(errno));
                    252:
                    253:        atf_tc_pass();
                    254: }
                    255:
                    256: ATF_TP_ADD_TCS(tp)
                    257: {
                    258:        ATF_TP_ADD_TC(tp, randlock);
                    259:        ATF_TP_ADD_TC(tp, deadlock);
                    260:
                    261:        return atf_no_error();
                    262: }

CVSweb <webmaster@jp.NetBSD.org>