Home | History | Annotate | Download | only in sched_setparam
      1 /*
      2  *  This program is free software; you can redistribute it and/or modify
      3  *  it under the terms of the GNU General Public License version 2.
      4  *
      5  *  This program is distributed in the hope that it will be useful,
      6  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      7  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      8  *  GNU General Public License for more details.
      9  *
     10  *
     11  * Test that sched_setparam() sets errno == ESRCH when no process can be found
     12  * corresponding to that specified by pid.
     13  *
     14  * The test create a child process which exit immediately and call
     15  * sched_setparam with the pid of defunct child.
     16  */
     17 #include <sched.h>
     18 #include <stdio.h>
     19 #include <errno.h>
     20 #include <unistd.h>
     21 #include <stdlib.h>
     22 #include <sys/wait.h>
     23 #include "posixtest.h"
     24 
     25 int main(void)
     26 {
     27 	struct sched_param param;
     28 	int result, child_pid, stat_loc;
     29 
     30 	if (sched_getparam(0, &param) == -1) {
     31 		perror("An error occurs when calling sched_getparam()");
     32 		return PTS_UNRESOLVED;
     33 	}
     34 
     35 	/* Create a child process which exit immediately */
     36 	child_pid = fork();
     37 	if (child_pid == -1) {
     38 		perror("An error occurs when calling fork()");
     39 		return PTS_UNRESOLVED;
     40 	} else if (child_pid == 0) {
     41 		exit(0);
     42 	}
     43 
     44 	/* Wait for the child process to exit */
     45 	if (wait(&stat_loc) == -1) {
     46 		perror("An error occurs when calling wait()");
     47 		return PTS_UNRESOLVED;
     48 	}
     49 
     50 	/* Assume the pid is not yet reatributed to an other process */
     51 	result = sched_setparam(child_pid, &param);
     52 
     53 	if (result == -1 && errno == ESRCH) {
     54 		printf("Test PASSED\n");
     55 		return PTS_PASS;
     56 	} else if (result != -1) {
     57 		printf("The returned code is not -1.\n");
     58 		return PTS_FAIL;
     59 	} else if (errno == EPERM) {
     60 		printf
     61 		    ("This process does not have the permission to invoke sched_setparam().\nTry to launch this test as root\n");
     62 		return PTS_UNRESOLVED;
     63 	} else {
     64 		perror("errno is not ESRCH");
     65 		return PTS_FAIL;
     66 	}
     67 }
     68