Home | History | Annotate | Line # | Download | only in posix_spawn
t_spawnattr.c revision 1.6.4.1
      1  1.6.4.1  perseant /* $NetBSD: t_spawnattr.c,v 1.6.4.1 2025/08/02 05:58:04 perseant Exp $ */
      2      1.1    martin 
      3      1.1    martin /*-
      4      1.1    martin  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5      1.1    martin  * All rights reserved.
      6      1.1    martin  *
      7      1.1    martin  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1    martin  * by Charles Zhang <charles (at) NetBSD.org> and
      9      1.1    martin  * Martin Husemann <martin (at) NetBSD.org>.
     10      1.1    martin  *
     11      1.1    martin  * Redistribution and use in source and binary forms, with or without
     12      1.1    martin  * modification, are permitted provided that the following conditions
     13      1.1    martin  * are met:
     14      1.1    martin  * 1. Redistributions of source code must retain the above copyright
     15      1.1    martin  *    notice, this list of conditions and the following disclaimer.
     16      1.1    martin  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.1    martin  *    notice, this list of conditions and the following disclaimer in the
     18      1.1    martin  *    documentation and/or other materials provided with the distribution.
     19      1.1    martin  *
     20      1.1    martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21      1.1    martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22      1.1    martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23      1.1    martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24      1.1    martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25      1.1    martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26      1.1    martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27      1.1    martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28      1.1    martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29      1.1    martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30      1.1    martin  * POSSIBILITY OF SUCH DAMAGE.
     31      1.1    martin  */
     32  1.6.4.1  perseant 
     33      1.5  christos #include <sys/cdefs.h>
     34  1.6.4.1  perseant __RCSID("$NetBSD: t_spawnattr.c,v 1.6.4.1 2025/08/02 05:58:04 perseant Exp $");
     35      1.1    martin 
     36      1.5  christos #include <sys/param.h>
     37  1.6.4.1  perseant #include <sys/wait.h>
     38  1.6.4.1  perseant 
     39      1.1    martin #include <atf-c.h>
     40      1.1    martin #include <errno.h>
     41      1.1    martin #include <fcntl.h>
     42      1.1    martin #include <sched.h>
     43      1.1    martin #include <signal.h>
     44      1.1    martin #include <spawn.h>
     45  1.6.4.1  perseant #include <stdio.h>
     46  1.6.4.1  perseant #include <stdlib.h>
     47  1.6.4.1  perseant #include <string.h>
     48      1.1    martin #include <unistd.h>
     49  1.6.4.1  perseant 
     50  1.6.4.1  perseant #include "h_macros.h"
     51      1.1    martin 
     52      1.1    martin static int
     53      1.3  christos get_different_scheduler(void)
     54      1.1    martin {
     55  1.6.4.1  perseant 	int sched;
     56  1.6.4.1  perseant 
     57      1.2  christos 	/*
     58      1.2  christos 	 * We don't want to use SCHED_OTHER because it does not have
     59      1.2  christos 	 * different priorities.
     60      1.2  christos 	 */
     61      1.1    martin 
     62      1.1    martin 	/* get current schedule policy */
     63  1.6.4.1  perseant 	RL(sched = sched_getscheduler(0));
     64  1.6.4.1  perseant 	switch (sched) {
     65      1.2  christos 	case SCHED_RR:
     66      1.2  christos 		return SCHED_FIFO;
     67      1.2  christos 	case SCHED_FIFO:
     68      1.2  christos 	case SCHED_OTHER:
     69      1.2  christos 		return SCHED_RR;
     70      1.2  christos 	default:
     71      1.2  christos 		abort();
     72      1.2  christos 	}
     73      1.1    martin }
     74      1.1    martin 
     75      1.1    martin static int
     76      1.3  christos get_different_priority(int scheduler)
     77      1.1    martin {
     78      1.3  christos 	int min, max, new, priority;
     79      1.1    martin 	struct sched_param param;
     80      1.1    martin 
     81      1.3  christos 	/* Get the priority range for the new scheduler */
     82  1.6.4.1  perseant 	RL(max = sched_get_priority_max(scheduler));
     83  1.6.4.1  perseant 	RL(min = sched_get_priority_min(scheduler));
     84      1.1    martin 
     85  1.6.4.1  perseant 	RL(sched_getparam(0, &param));
     86      1.1    martin 	priority = param.sched_priority;
     87  1.6.4.1  perseant 
     88      1.1    martin 	/* new schedule policy */
     89      1.3  christos 	for (new = min; new <= max; new++)
     90      1.3  christos 		if (priority != new)
     91      1.3  christos 			break;
     92  1.6.4.1  perseant 
     93      1.3  christos 	ATF_REQUIRE_MSG(priority != new, "could not find different priority");
     94      1.3  christos 	printf("min %d max %d for scheduler %d, returning %d\n",
     95      1.3  christos 	    min, max, scheduler, new);
     96      1.1    martin 	return new;
     97      1.1    martin }
     98      1.1    martin 
     99      1.1    martin ATF_TC(t_spawnattr);
    100      1.1    martin ATF_TC_HEAD(t_spawnattr, tc)
    101      1.1    martin {
    102      1.1    martin 	atf_tc_set_md_var(tc, "require.user", "root");
    103      1.1    martin 	atf_tc_set_md_var(tc, "descr",
    104      1.1    martin 	    "Tests posix_spawn with scheduler attributes");
    105      1.1    martin }
    106      1.1    martin ATF_TC_BODY(t_spawnattr, tc)
    107      1.1    martin {
    108  1.6.4.1  perseant 	int pid, scheduler, child_scheduler, priority, status, pfd[2];
    109      1.1    martin 	char helper_arg[128];
    110      1.1    martin 	char * const args[] = { __UNCONST("h_spawnattr"), helper_arg, NULL };
    111      1.1    martin 	struct sched_param sp, child_sp;
    112      1.1    martin 	sigset_t sig;
    113      1.1    martin 	posix_spawnattr_t attr;
    114      1.1    martin 	char helper[FILENAME_MAX];
    115      1.1    martin 
    116      1.1    martin 	/*
    117      1.6    andvar 	 * create a pipe to control the child
    118      1.1    martin 	 */
    119  1.6.4.1  perseant 	RL(pipe(pfd));
    120  1.6.4.1  perseant 	snprintf(helper_arg, sizeof(helper_arg), "%d", pfd[0]);
    121      1.1    martin 
    122  1.6.4.1  perseant 	RZ(posix_spawnattr_init(&attr));
    123      1.1    martin 
    124      1.1    martin 	scheduler = get_different_scheduler();
    125      1.3  christos 	priority = get_different_priority(scheduler);
    126      1.1    martin 	sp.sched_priority = priority;
    127      1.3  christos 	printf("using scheduler %d, priority %d\n", scheduler, priority);
    128      1.1    martin 
    129  1.6.4.1  perseant 	RL(sigemptyset(&sig));
    130  1.6.4.1  perseant 	RL(sigaddset(&sig, SIGUSR1));
    131  1.6.4.1  perseant 
    132  1.6.4.1  perseant 	RZ(posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDULER |
    133      1.1    martin 		POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETPGROUP |
    134      1.1    martin 		POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
    135  1.6.4.1  perseant 		POSIX_SPAWN_SETSIGDEF));
    136  1.6.4.1  perseant 	RZ(posix_spawnattr_setpgroup(&attr, 0));
    137  1.6.4.1  perseant 	RZ(posix_spawnattr_setschedparam(&attr, &sp));
    138  1.6.4.1  perseant 	RZ(posix_spawnattr_setschedpolicy(&attr, scheduler));
    139  1.6.4.1  perseant 	RZ(posix_spawnattr_setsigmask(&attr, &sig));
    140  1.6.4.1  perseant 	RZ(posix_spawnattr_setsigdefault(&attr, &sig));
    141      1.1    martin 
    142  1.6.4.1  perseant 	snprintf(helper, sizeof(helper), "%s/h_spawnattr",
    143      1.1    martin 	    atf_tc_get_config_var(tc, "srcdir"));
    144  1.6.4.1  perseant 	RZ(posix_spawn(&pid, helper, NULL, &attr, args, NULL));
    145  1.6.4.1  perseant 	ATF_REQUIRE_MSG(pid > 0, "pid=%lld", (long long)pid);
    146      1.1    martin 
    147  1.6.4.1  perseant 	RL(child_scheduler = sched_getscheduler(pid));
    148      1.1    martin 	ATF_REQUIRE_MSG(scheduler == child_scheduler,
    149      1.1    martin 	    "scheduler = %d, child_scheduler = %d, pid %d, errno %d",
    150      1.1    martin 	    scheduler, child_scheduler, pid, errno);
    151      1.1    martin 
    152  1.6.4.1  perseant 	RL(sched_getparam(pid, &child_sp));
    153      1.1    martin 	ATF_REQUIRE_MSG(child_sp.sched_priority == sp.sched_priority,
    154      1.1    martin 	    "priority is: %d, but we requested: %d",
    155      1.1    martin 	    child_sp.sched_priority, sp.sched_priority);
    156      1.1    martin 
    157      1.1    martin 	ATF_REQUIRE_MSG(pid == getpgid(pid), "child pid: %d, child pgid: %d",
    158      1.1    martin 	    pid, getpgid(pid));
    159      1.1    martin 
    160      1.1    martin 	/* ready, let child go */
    161  1.6.4.1  perseant 	RL(write(pfd[1], "q", 1));
    162  1.6.4.1  perseant 	RL(close(pfd[0]));
    163  1.6.4.1  perseant 	RL(close(pfd[1]));
    164      1.1    martin 
    165      1.1    martin 	/* wait and check result from child */
    166  1.6.4.1  perseant 	RL(waitpid(pid, &status, 0));
    167      1.1    martin 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
    168      1.1    martin 
    169  1.6.4.1  perseant 	RZ(posix_spawnattr_destroy(&attr));
    170      1.1    martin }
    171      1.1    martin 
    172      1.4    martin ATF_TC(t_spawn_resetids);
    173      1.4    martin 
    174      1.4    martin ATF_TC_HEAD(t_spawn_resetids, tc)
    175      1.4    martin {
    176      1.4    martin 	atf_tc_set_md_var(tc, "descr",
    177      1.4    martin 	    "posix_spawn a child and with POSIX_SPAWN_RESETIDS flag");
    178      1.4    martin }
    179      1.4    martin 
    180      1.4    martin ATF_TC_BODY(t_spawn_resetids, tc)
    181      1.4    martin {
    182      1.4    martin 	char buf[FILENAME_MAX];
    183      1.4    martin 	char * const args[] = {
    184      1.4    martin 	     __UNCONST("h_spawn"), __UNCONST("--resetids"), NULL
    185      1.4    martin 	};
    186      1.4    martin 	posix_spawnattr_t attr;
    187  1.6.4.1  perseant 	int status;
    188      1.4    martin 	pid_t pid;
    189      1.4    martin 
    190  1.6.4.1  perseant 	RZ(posix_spawnattr_init(&attr));
    191  1.6.4.1  perseant 	RZ(posix_spawnattr_setflags(&attr, POSIX_SPAWN_RESETIDS));
    192      1.4    martin 
    193  1.6.4.1  perseant 	snprintf(buf, sizeof(buf), "%s/h_spawn",
    194      1.4    martin 	    atf_tc_get_config_var(tc, "srcdir"));
    195      1.4    martin 
    196  1.6.4.1  perseant 	RZ(posix_spawn(&pid, buf, NULL, &attr, args, NULL));
    197  1.6.4.1  perseant 	ATF_REQUIRE_MSG(pid > 0, "pid=%lld", (long long)pid);
    198  1.6.4.1  perseant 	RL(waitpid(pid, &status, 0));
    199  1.6.4.1  perseant 	ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
    200  1.6.4.1  perseant 	    "status=0x%x", status);
    201      1.4    martin 
    202  1.6.4.1  perseant 	RZ(posix_spawnattr_destroy(&attr));
    203      1.4    martin }
    204      1.4    martin 
    205      1.1    martin ATF_TP_ADD_TCS(tp)
    206      1.1    martin {
    207      1.1    martin 	ATF_TP_ADD_TC(tp, t_spawnattr);
    208      1.4    martin 	ATF_TP_ADD_TC(tp, t_spawn_resetids);
    209      1.1    martin 
    210      1.1    martin 	return atf_no_error();
    211      1.1    martin }
    212