Home | History | Annotate | Line # | Download | only in posix_spawn
t_spawnattr.c revision 1.1
      1  1.1  martin /* $NetBSD: t_spawnattr.c,v 1.1 2012/02/13 21:03:08 martin 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 get_different_scheduler(void);
     49  1.1  martin static int get_different_priority(void);
     50  1.1  martin 
     51  1.1  martin static int
     52  1.1  martin get_different_scheduler()
     53  1.1  martin {
     54  1.1  martin 	int scheduler, max, min, new;
     55  1.1  martin 
     56  1.1  martin 	max = MAX(MAX(SCHED_FIFO, SCHED_OTHER), SCHED_RR);
     57  1.1  martin 	min = MIN(MIN(SCHED_FIFO, SCHED_OTHER), SCHED_RR);
     58  1.1  martin 
     59  1.1  martin 	/* get current schedule policy */
     60  1.1  martin 	scheduler = sched_getscheduler(0);
     61  1.1  martin 
     62  1.1  martin 	/* new scheduler */
     63  1.1  martin 	new = (scheduler + 1);
     64  1.1  martin 	if (new > max)
     65  1.1  martin 		new = min;
     66  1.1  martin 
     67  1.1  martin 	return new;
     68  1.1  martin }
     69  1.1  martin 
     70  1.1  martin static int
     71  1.1  martin get_different_priority()
     72  1.1  martin {
     73  1.1  martin 	int scheduler, max, min, new, priority;
     74  1.1  martin 	struct sched_param param;
     75  1.1  martin 
     76  1.1  martin 	/* get current schedule policy */
     77  1.1  martin 	scheduler = sched_getscheduler(0);
     78  1.1  martin 
     79  1.1  martin 	max = sched_get_priority_max(scheduler);
     80  1.1  martin 	min = sched_get_priority_min(scheduler);
     81  1.1  martin 
     82  1.1  martin 	sched_getparam(0, &param);
     83  1.1  martin 	priority = param.sched_priority;
     84  1.1  martin 
     85  1.1  martin 	/* new schedule policy */
     86  1.1  martin 	new = (priority + 1);
     87  1.1  martin 	if (new > max)
     88  1.1  martin 		new = min;
     89  1.1  martin 
     90  1.1  martin 	return new;
     91  1.1  martin }
     92  1.1  martin 
     93  1.1  martin ATF_TC(t_spawnattr);
     94  1.1  martin 
     95  1.1  martin ATF_TC_HEAD(t_spawnattr, tc)
     96  1.1  martin {
     97  1.1  martin 	atf_tc_set_md_var(tc, "require.user", "root");
     98  1.1  martin 	atf_tc_set_md_var(tc, "descr",
     99  1.1  martin 	    "Tests posix_spawn with scheduler attributes");
    100  1.1  martin }
    101  1.1  martin 
    102  1.1  martin ATF_TC_BODY(t_spawnattr, tc)
    103  1.1  martin {
    104  1.1  martin 	int pid, scheduler, child_scheduler, priority, status, err, pfd[2];
    105  1.1  martin 	char helper_arg[128];
    106  1.1  martin 	char * const args[] = { __UNCONST("h_spawnattr"), helper_arg, NULL };
    107  1.1  martin 	struct sched_param sp, child_sp;
    108  1.1  martin 	sigset_t sig;
    109  1.1  martin 	posix_spawnattr_t attr;
    110  1.1  martin 	char helper[FILENAME_MAX];
    111  1.1  martin 
    112  1.1  martin 	/*
    113  1.1  martin 	 * create a pipe to controll the child
    114  1.1  martin 	 */
    115  1.1  martin 	err = pipe(pfd);
    116  1.1  martin 	ATF_REQUIRE_MSG(err == 0, "could not create pipe, errno %d", errno);
    117  1.1  martin 	sprintf(helper_arg, "%d", pfd[0]);
    118  1.1  martin 
    119  1.1  martin 	posix_spawnattr_init(&attr);
    120  1.1  martin 
    121  1.1  martin 	scheduler = get_different_scheduler();
    122  1.1  martin 	priority = get_different_priority();
    123  1.1  martin 	sp.sched_priority = 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