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