1272343Sngie/* $NetBSD: t_spawnattr.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2012 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Charles Zhang <charles@NetBSD.org> and 9272343Sngie * Martin Husemann <martin@NetBSD.org>. 10272343Sngie * 11272343Sngie * Redistribution and use in source and binary forms, with or without 12272343Sngie * modification, are permitted provided that the following conditions 13272343Sngie * are met: 14272343Sngie * 1. Redistributions of source code must retain the above copyright 15272343Sngie * notice, this list of conditions and the following disclaimer. 16272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 17272343Sngie * notice, this list of conditions and the following disclaimer in the 18272343Sngie * documentation and/or other materials provided with the distribution. 19272343Sngie * 20272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30272343Sngie * POSSIBILITY OF SUCH DAMAGE. 31272343Sngie */ 32272343Sngie 33287360Skib#include <sys/param.h> 34272343Sngie#include <atf-c.h> 35272343Sngie#include <stdio.h> 36272343Sngie#include <stdlib.h> 37272343Sngie#include <string.h> 38272343Sngie#include <errno.h> 39272343Sngie#include <fcntl.h> 40272343Sngie#include <sched.h> 41272343Sngie#include <signal.h> 42272343Sngie#include <spawn.h> 43272343Sngie#include <unistd.h> 44272343Sngie#include <sys/wait.h> 45272343Sngie 46272343Sngiestatic int get_different_scheduler(void); 47287360Skibstatic int get_different_priority(int scheduler); 48272343Sngie 49287360Skibstatic const int schedulers[] = { 50287360Skib SCHED_OTHER, 51287360Skib SCHED_FIFO, 52287360Skib SCHED_RR 53287360Skib}; 54287360Skib 55272343Sngiestatic int 56287360Skibget_different_scheduler(void) 57272343Sngie{ 58287360Skib u_int i; 59287360Skib int scheduler; 60272343Sngie 61272343Sngie /* get current schedule policy */ 62272343Sngie scheduler = sched_getscheduler(0); 63287360Skib for (i = 0; i < nitems(schedulers); i++) { 64287360Skib if (schedulers[i] == scheduler) 65287360Skib break; 66287360Skib } 67287360Skib ATF_REQUIRE_MSG(i < nitems(schedulers), 68287360Skib "Unknown current scheduler %d", scheduler); 69272343Sngie 70272343Sngie /* new scheduler */ 71287360Skib i++; 72287360Skib if (i >= nitems(schedulers)) 73287360Skib i = 0; 74287360Skib return schedulers[i]; 75272343Sngie} 76272343Sngie 77272343Sngiestatic int 78287360Skibget_different_priority(int scheduler) 79272343Sngie{ 80287360Skib int max, min, new, priority; 81272343Sngie struct sched_param param; 82272343Sngie 83272343Sngie max = sched_get_priority_max(scheduler); 84272343Sngie min = sched_get_priority_min(scheduler); 85272343Sngie 86272343Sngie sched_getparam(0, ¶m); 87272343Sngie priority = param.sched_priority; 88272343Sngie 89287360Skib /* 90287360Skib * Change numerical value of the priority, to ensure that it 91287360Skib * was set for the spawned child. 92287360Skib */ 93287360Skib new = priority + 1; 94272343Sngie if (new > max) 95272343Sngie new = min; 96272343Sngie return new; 97272343Sngie} 98272343Sngie 99272343SngieATF_TC(t_spawnattr); 100272343Sngie 101272343SngieATF_TC_HEAD(t_spawnattr, tc) 102272343Sngie{ 103272343Sngie atf_tc_set_md_var(tc, "require.user", "root"); 104272343Sngie atf_tc_set_md_var(tc, "descr", 105272343Sngie "Tests posix_spawn with scheduler attributes"); 106272343Sngie} 107272343Sngie 108272343SngieATF_TC_BODY(t_spawnattr, tc) 109272343Sngie{ 110272343Sngie int pid, scheduler, child_scheduler, priority, status, err, pfd[2]; 111272343Sngie char helper_arg[128]; 112272343Sngie char * const args[] = { __UNCONST("h_spawnattr"), helper_arg, NULL }; 113272343Sngie struct sched_param sp, child_sp; 114272343Sngie sigset_t sig; 115272343Sngie posix_spawnattr_t attr; 116272343Sngie char helper[FILENAME_MAX]; 117272343Sngie 118272343Sngie /* 119272343Sngie * create a pipe to controll the child 120272343Sngie */ 121272343Sngie err = pipe(pfd); 122272343Sngie ATF_REQUIRE_MSG(err == 0, "could not create pipe, errno %d", errno); 123272343Sngie sprintf(helper_arg, "%d", pfd[0]); 124272343Sngie 125272343Sngie posix_spawnattr_init(&attr); 126272343Sngie 127272343Sngie scheduler = get_different_scheduler(); 128287360Skib priority = get_different_priority(scheduler); 129272343Sngie sp.sched_priority = priority; 130272343Sngie 131272343Sngie sigemptyset(&sig); 132272343Sngie sigaddset(&sig, SIGUSR1); 133272343Sngie 134287360Skib posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDULER | 135287360Skib POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETPGROUP | 136287360Skib POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF); 137272343Sngie posix_spawnattr_setpgroup(&attr, 0); 138272343Sngie posix_spawnattr_setschedparam(&attr, &sp); 139272343Sngie posix_spawnattr_setschedpolicy(&attr, scheduler); 140272343Sngie posix_spawnattr_setsigmask(&attr, &sig); 141272343Sngie posix_spawnattr_setsigdefault(&attr, &sig); 142272343Sngie 143272343Sngie sprintf(helper, "%s/h_spawnattr", 144272343Sngie atf_tc_get_config_var(tc, "srcdir")); 145272343Sngie err = posix_spawn(&pid, helper, NULL, &attr, args, NULL); 146272343Sngie ATF_REQUIRE_MSG(err == 0, "error %d", err); 147272343Sngie 148272343Sngie child_scheduler = sched_getscheduler(pid); 149272343Sngie ATF_REQUIRE_MSG(scheduler == child_scheduler, 150272343Sngie "scheduler = %d, child_scheduler = %d, pid %d, errno %d", 151272343Sngie scheduler, child_scheduler, pid, errno); 152272343Sngie 153272343Sngie sched_getparam(pid, &child_sp); 154272343Sngie ATF_REQUIRE_MSG(child_sp.sched_priority == sp.sched_priority, 155272343Sngie "priority is: %d, but we requested: %d", 156272343Sngie child_sp.sched_priority, sp.sched_priority); 157272343Sngie 158272343Sngie ATF_REQUIRE_MSG(pid == getpgid(pid), "child pid: %d, child pgid: %d", 159272343Sngie pid, getpgid(pid)); 160272343Sngie 161272343Sngie /* ready, let child go */ 162272343Sngie write(pfd[1], "q", 1); 163272343Sngie close(pfd[0]); 164272343Sngie close(pfd[1]); 165272343Sngie 166272343Sngie /* wait and check result from child */ 167272343Sngie waitpid(pid, &status, 0); 168272343Sngie ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); 169272343Sngie 170272343Sngie posix_spawnattr_destroy(&attr); 171272343Sngie} 172272343Sngie 173272343SngieATF_TP_ADD_TCS(tp) 174272343Sngie{ 175272343Sngie ATF_TP_ADD_TC(tp, t_spawnattr); 176272343Sngie 177272343Sngie return atf_no_error(); 178272343Sngie} 179