Home | History | Annotate | Line # | Download | only in kernel
t_semtimedop.c revision 1.1
      1 /*	$NetBSD: t_semtimedop.c,v 1.1 2024/10/03 17:11:14 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2024 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 #include <sys/cdefs.h>
     29 __RCSID("$NetBSD: t_semtimedop.c,v 1.1 2024/10/03 17:11:14 christos Exp $");
     30 
     31 #include <sys/types.h>
     32 #include <sys/ipc.h>
     33 #include <sys/sem.h>
     34 #include <errno.h>
     35 #include <string.h>
     36 #include <time.h>
     37 #include <stdlib.h>
     38 #include <stdio.h>
     39 #include <unistd.h>
     40 #include <atf-c.h>
     41 #include <sys/wait.h>
     42 
     43 ATF_TC(semtimedop_basic);
     44 ATF_TC_HEAD(semtimedop_basic, tc)
     45 {
     46 	atf_tc_set_md_var(tc, "descr", "Basic semtimedop functionality");
     47 }
     48 
     49 ATF_TC_BODY(semtimedop_basic, tc)
     50 {
     51 	key_t		key = IPC_PRIVATE;
     52 	int		semid;
     53 	struct sembuf	sops;
     54 	struct timespec	timeout;
     55 
     56 	/* Create semaphore set with 1 semaphore */
     57 	semid = semget(key, 1, IPC_CREAT | 0600);
     58 	ATF_REQUIRE_MSG(semid != -1, "semget failed: %s", strerror(errno));
     59 
     60 	/* Initialize semaphore to 0 */
     61 	if (semctl(semid, 0, SETVAL, 0) == -1) {
     62 		ATF_REQUIRE_MSG(0, "semctl SETVAL failed: %s",
     63 		    strerror(errno));
     64 	}
     65 
     66 	/* Define semtimedop operation: increment semaphore */
     67 	sops.sem_num = 0;
     68 	sops.sem_op = 1;
     69 	sops.sem_flg = 0;
     70 
     71 	/* Define timeout */
     72 	timeout.tv_sec = 1;
     73 	timeout.tv_nsec = 0;
     74 
     75 	/* Perform semtimedop */
     76 	if (semtimedop(semid, &sops, 1, &timeout) == -1) {
     77 		ATF_REQUIRE_MSG(0, "semtimedop failed: %s", strerror(errno));
     78 	}
     79 
     80 	/* Check semaphore value */
     81 	int val = semctl(semid, 0, GETVAL);
     82 	ATF_REQUIRE_MSG(val == 1,
     83 	    "Semaphore value incorrect: got %d, expected 1", val);
     84 
     85 	/* Clean up */
     86 	ATF_REQUIRE_MSG(semctl(semid, 0, IPC_RMID) != -1,
     87 	    "semctl IPC_RMID failed: %s", strerror(errno));
     88 }
     89 
     90 /* semtimedop blocks until timeout expires */
     91 ATF_TC(semtimedop_timeout);
     92 ATF_TC_HEAD(semtimedop_timeout, tc)
     93 {
     94 	atf_tc_set_md_var(tc, "descr",
     95 	    "semtimedop blocks until timeout expires");
     96 }
     97 
     98 ATF_TC_BODY(semtimedop_timeout, tc)
     99 {
    100 	key_t		key = IPC_PRIVATE;
    101 	int		semid;
    102 	struct sembuf	sops;
    103 	struct timespec	timeout;
    104 	pid_t		pid;
    105 	int		status;
    106 
    107 	/* Create semaphore set with 1 semaphore */
    108 	semid = semget(key, 1, IPC_CREAT | 0600);
    109 	ATF_REQUIRE_MSG(semid != -1, "semget failed: %s", strerror(errno));
    110 
    111 	/* Initialize semaphore to 0 */
    112 	if (semctl(semid, 0, SETVAL, 0) == -1) {
    113 		ATF_REQUIRE_MSG(0, "semctl SETVAL failed: %s", strerror(errno));
    114 	}
    115 
    116 	pid = fork();
    117 	ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
    118 
    119 	if (pid == 0) {
    120 		/*
    121 		 * Child: perform semtimedop with negative sem_op, should
    122 		 * block until timeout
    123 		 */
    124 		sops.sem_num = 0;
    125 		sops.sem_op = -1;
    126 		sops.sem_flg = 0;
    127 
    128 		timeout.tv_sec = 2;
    129 		timeout.tv_nsec = 0;
    130 
    131 		if (semtimedop(semid, &sops, 1, &timeout) == -1) {
    132 			if (errno == EAGAIN || errno == EINTR) {
    133 				exit(0);	/* Expected */
    134 			}
    135 		}
    136 		exit(1);	/* Unexpected failure/success */
    137 	}
    138 
    139 	/* Parent: wait for child to finish */
    140 	waitpid(pid, &status, 0);
    141 	ATF_REQUIRE(WIFEXITED(status));
    142 	ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
    143 
    144 	/* Clean up */
    145 	ATF_REQUIRE_MSG(semctl(semid, 0, IPC_RMID) != -1,
    146 	    "semctl IPC_RMID failed: %s", strerror(errno));
    147 }
    148 
    149 /* semtimedop with SEM_UNDO adjusts semaphore on exit */
    150 ATF_TC(semtimedop_semundo);
    151 ATF_TC_HEAD(semtimedop_semundo, tc)
    152 {
    153 	atf_tc_set_md_var(tc, "descr",
    154 	    "semtimedop with SEM_UNDO adjusts semaphore on exit");
    155 }
    156 
    157 ATF_TC_BODY(semtimedop_semundo, tc)
    158 {
    159 	key_t		key = IPC_PRIVATE;
    160 	int		semid;
    161 	struct sembuf	sops;
    162 	struct timespec	timeout;
    163 	pid_t		pid;
    164 	int		val;
    165 
    166 	/* Create semaphore set with 1 semaphore */
    167 	semid = semget(key, 1, IPC_CREAT | 0600);
    168 	ATF_REQUIRE_MSG(semid != -1, "semget failed: %s", strerror(errno));
    169 
    170 	/* Initialize semaphore to 0 */
    171 	if (semctl(semid, 0, SETVAL, 0) == -1) {
    172 		ATF_REQUIRE_MSG(0, "semctl SETVAL failed: %s", strerror(errno));
    173 	}
    174 
    175 	pid = fork();
    176 	ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
    177 
    178 	if (pid == 0) {
    179 		/* Child: perform semtimedop with SEM_UNDO */
    180 		sops.sem_num = 0;
    181 		sops.sem_op = 1;
    182 		sops.sem_flg = SEM_UNDO;
    183 
    184 		timeout.tv_sec = 1;
    185 		timeout.tv_nsec = 0;
    186 
    187 		if (semtimedop(semid, &sops, 1, &timeout) == -1) {
    188 			exit(1);	/* Unexpected failure */
    189 		}
    190 
    191 		exit(0); /* Exit normally, SEM_UNDO should be triggered */
    192 	}
    193 
    194 	/* Parent: wait for child to exit */
    195 	waitpid(pid, NULL, 0);
    196 
    197 	/* Check semaphore value; should be 0 after SEM_UNDO */
    198 	val = semctl(semid, 0, GETVAL);
    199 	ATF_REQUIRE_MSG(val == 0,
    200 	    "Semaphore value incorrect after SEM_UNDO: got %d, "
    201 	    "expected 0", val);
    202 
    203 	/* Clean up */
    204 	ATF_REQUIRE_MSG(semctl(semid, 0, IPC_RMID) != -1,
    205 	    "semctl IPC_RMID failed: %s", strerror(errno));
    206 }
    207 
    208 /* semtimedop handles invalid parameters correctly */
    209 ATF_TC(semtimedop_invalid);
    210 ATF_TC_HEAD(semtimedop_invalid, tc)
    211 {
    212 	atf_tc_set_md_var(tc, "descr",
    213 	    "semtimedop handles invalid parameters correctly");
    214 }
    215 
    216 ATF_TC_BODY(semtimedop_invalid, tc)
    217 {
    218 	struct sembuf	sops;
    219 	struct timespec	timeout;
    220 
    221 	/* Invalid semaphore id */
    222 	sops.sem_num = 0;
    223 	sops.sem_op = -1;
    224 	sops.sem_flg = 0;
    225 
    226 	timeout.tv_sec = 1;
    227 	timeout.tv_nsec = 0;
    228 
    229 	/* Attempt to perform semtimedop on invalid semid */
    230 	ATF_REQUIRE_MSG(semtimedop(-1, &sops, 1, &timeout) == -1
    231 	    && errno == EINVAL, "semtimedop did not fail on invalid semid");
    232 
    233 	/* Create semaphore set */
    234 	key_t	key = IPC_PRIVATE;
    235 	int	semid = semget(key, 1, IPC_CREAT | 0600);
    236 	ATF_REQUIRE_MSG(semid != -1, "semget failed: %s", strerror(errno));
    237 
    238 	/* Initialize semaphore to 0 */
    239 	if (semctl(semid, 0, SETVAL, 0) == -1) {
    240 		ATF_REQUIRE_MSG(0, "semctl SETVAL failed: %s", strerror(errno));
    241 	}
    242 
    243 	/* Set an invalid semaphore number */
    244 	sops.sem_num = 1;	/* Only 1 semaphore in set, index 0 */
    245 	sops.sem_op = 1;
    246 	sops.sem_flg = 0;
    247 
    248 	/* Attempt semtimedop with invalid sem_num */
    249 	ATF_REQUIRE_MSG(semtimedop(semid, &sops, 1, &timeout) == -1
    250 	    && errno == EFBIG, "semtimedop did not fail on invalid sem_num");
    251 
    252 	/* Clean up */
    253 	ATF_REQUIRE_MSG(semctl(semid, 0, IPC_RMID) != -1,
    254 	    "semctl IPC_RMID failed: %s", strerror(errno));
    255 }
    256 
    257 ATF_TP_ADD_TCS(tp)
    258 {
    259 	ATF_TP_ADD_TC(tp, semtimedop_basic);
    260 	ATF_TP_ADD_TC(tp, semtimedop_timeout);
    261 	ATF_TP_ADD_TC(tp, semtimedop_semundo);
    262 	ATF_TP_ADD_TC(tp, semtimedop_invalid);
    263 
    264 	return atf_no_error();
    265 }
    266