Home | History | Annotate | Line # | Download | only in posix_spawn
t_spawnattr.c revision 1.3
      1  1.3  christos /* $NetBSD: t_spawnattr.c,v 1.3 2017/12/21 03:31:43 christos 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.1    martin 
     33  1.1    martin #include <atf-c.h>
     34  1.1    martin #include <stdio.h>
     35  1.1    martin #include <stdlib.h>
     36  1.1    martin #include <string.h>
     37  1.1    martin #include <errno.h>
     38  1.1    martin #include <fcntl.h>
     39  1.1    martin #include <sched.h>
     40  1.1    martin #include <signal.h>
     41  1.1    martin #include <spawn.h>
     42  1.1    martin #include <unistd.h>
     43  1.1    martin #include <sys/wait.h>
     44  1.1    martin 
     45  1.1    martin #define MAX(a, b)	(a) > (b) ? (a) : (b)
     46  1.1    martin #define MIN(a, b)	(a) > (b) ? (b) : (a)
     47  1.1    martin 
     48  1.1    martin static int
     49  1.3  christos get_different_scheduler(void)
     50  1.1    martin {
     51  1.2  christos 	/*
     52  1.2  christos 	 * We don't want to use SCHED_OTHER because it does not have
     53  1.2  christos 	 * different priorities.
     54  1.2  christos 	 */
     55  1.1    martin 
     56  1.1    martin 	/* get current schedule policy */
     57  1.2  christos 	switch (sched_getscheduler(0)) {
     58  1.2  christos 	case SCHED_RR:
     59  1.2  christos 		return SCHED_FIFO;
     60  1.2  christos 	case SCHED_FIFO:
     61  1.2  christos 	case SCHED_OTHER:
     62  1.2  christos 		return SCHED_RR;
     63  1.2  christos 	default:
     64  1.2  christos 		abort();
     65  1.2  christos 	}
     66  1.1    martin }
     67  1.1    martin 
     68  1.1    martin static int
     69  1.3  christos get_different_priority(int scheduler)
     70  1.1    martin {
     71  1.3  christos 	int min, max, new, priority;
     72  1.1    martin 	struct sched_param param;
     73  1.1    martin 
     74  1.3  christos 	/* Get the priority range for the new scheduler */
     75  1.1    martin 	max = sched_get_priority_max(scheduler);
     76  1.1    martin 	min = sched_get_priority_min(scheduler);
     77  1.1    martin 
     78  1.1    martin 	sched_getparam(0, &param);
     79  1.1    martin 	priority = param.sched_priority;
     80  1.1    martin 
     81  1.1    martin 	/* new schedule policy */
     82  1.3  christos 	for (new = min; new <= max; new++)
     83  1.3  christos 		if (priority != new)
     84  1.3  christos 			break;
     85  1.1    martin 
     86  1.3  christos 	ATF_REQUIRE_MSG(priority != new, "could not find different priority");
     87  1.3  christos 	printf("min %d max %d for scheduler %d, returning %d\n",
     88  1.3  christos 	    min, max, scheduler, new);
     89  1.1    martin 	return new;
     90  1.1    martin }
     91  1.1    martin 
     92  1.1    martin ATF_TC(t_spawnattr);
     93  1.1    martin 
     94  1.1    martin ATF_TC_HEAD(t_spawnattr, tc)
     95  1.1    martin {
     96  1.1    martin 	atf_tc_set_md_var(tc, "require.user", "root");
     97  1.1    martin 	atf_tc_set_md_var(tc, "descr",
     98  1.1    martin 	    "Tests posix_spawn with scheduler attributes");
     99  1.1    martin }
    100  1.1    martin 
    101  1.1    martin ATF_TC_BODY(t_spawnattr, tc)
    102  1.1    martin {
    103  1.1    martin 	int pid, scheduler, child_scheduler, priority, status, err, pfd[2];
    104  1.1    martin 	char helper_arg[128];
    105  1.1    martin 	char * const args[] = { __UNCONST("h_spawnattr"), helper_arg, NULL };
    106  1.1    martin 	struct sched_param sp, child_sp;
    107  1.1    martin 	sigset_t sig;
    108  1.1    martin 	posix_spawnattr_t attr;
    109  1.1    martin 	char helper[FILENAME_MAX];
    110  1.1    martin 
    111  1.1    martin 	/*
    112  1.1    martin 	 * create a pipe to controll the child
    113  1.1    martin 	 */
    114  1.1    martin 	err = pipe(pfd);
    115  1.1    martin 	ATF_REQUIRE_MSG(err == 0, "could not create pipe, errno %d", errno);
    116  1.1    martin 	sprintf(helper_arg, "%d", pfd[0]);
    117  1.1    martin 
    118  1.1    martin 	posix_spawnattr_init(&attr);
    119  1.1    martin 
    120  1.1    martin 	scheduler = get_different_scheduler();
    121  1.3  christos 	priority = get_different_priority(scheduler);
    122  1.1    martin 	sp.sched_priority = priority;
    123  1.3  christos 	printf("using scheduler %d, priority %d\n", scheduler, priority);
    124  1.1    martin 
    125  1.1    martin 	sigemptyset(&sig);
    126  1.1    martin 	sigaddset(&sig, SIGUSR1);
    127  1.1    martin 
    128  1.1    martin 	posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDULER |
    129  1.1    martin 		POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETPGROUP |
    130  1.1    martin 		POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
    131  1.1    martin 		POSIX_SPAWN_SETSIGDEF);
    132  1.1    martin 	posix_spawnattr_setpgroup(&attr, 0);
    133  1.1    martin 	posix_spawnattr_setschedparam(&attr, &sp);
    134  1.1    martin 	posix_spawnattr_setschedpolicy(&attr, scheduler);
    135  1.1    martin 	posix_spawnattr_setsigmask(&attr, &sig);
    136  1.1    martin 	posix_spawnattr_setsigdefault(&attr, &sig);
    137  1.1    martin 
    138  1.1    martin 	sprintf(helper, "%s/h_spawnattr",
    139  1.1    martin 	    atf_tc_get_config_var(tc, "srcdir"));
    140  1.1    martin 	err = posix_spawn(&pid, helper, NULL, &attr, args, NULL);
    141  1.1    martin 	ATF_REQUIRE_MSG(err == 0, "error %d", err);
    142  1.1    martin 
    143  1.1    martin 	child_scheduler = sched_getscheduler(pid);
    144  1.1    martin 	ATF_REQUIRE_MSG(scheduler == child_scheduler,
    145  1.1    martin 	    "scheduler = %d, child_scheduler = %d, pid %d, errno %d",
    146  1.1    martin 	    scheduler, child_scheduler, pid, errno);
    147  1.1    martin 
    148  1.1    martin 	sched_getparam(pid, &child_sp);
    149  1.1    martin 	ATF_REQUIRE_MSG(child_sp.sched_priority == sp.sched_priority,
    150  1.1    martin 	    "priority is: %d, but we requested: %d",
    151  1.1    martin 	    child_sp.sched_priority, sp.sched_priority);
    152  1.1    martin 
    153  1.1    martin 	ATF_REQUIRE_MSG(pid == getpgid(pid), "child pid: %d, child pgid: %d",
    154  1.1    martin 	    pid, getpgid(pid));
    155  1.1    martin 
    156  1.1    martin 	/* ready, let child go */
    157  1.1    martin 	write(pfd[1], "q", 1);
    158  1.1    martin 	close(pfd[0]);
    159  1.1    martin 	close(pfd[1]);
    160  1.1    martin 
    161  1.1    martin 	/* wait and check result from child */
    162  1.1    martin 	waitpid(pid, &status, 0);
    163  1.1    martin 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
    164  1.1    martin 
    165  1.1    martin 	posix_spawnattr_destroy(&attr);
    166  1.1    martin }
    167  1.1    martin 
    168  1.1    martin ATF_TP_ADD_TCS(tp)
    169  1.1    martin {
    170  1.1    martin 	ATF_TP_ADD_TC(tp, t_spawnattr);
    171  1.1    martin 
    172  1.1    martin 	return atf_no_error();
    173  1.1    martin }
    174