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_getparam() sets errno == EPERM if the requesting process
12 * does not have permission.
13 */
14
15 /* adam.li@intel.com - 2004-05-21
16  *
17  * On Linux, e.g, the kernel makes no check on user permission to call this
18  * API. So basically we don't know on what condition a system should return
19  * EPERM. It is implementation defined.
20  */
21
22#define _XOPEN_SOURCE 600
23#include <stdio.h>
24#include <sched.h>
25#include <errno.h>
26#include <unistd.h>
27#include <sys/types.h>
28#include <pwd.h>
29#include <string.h>
30#include "posixtest.h"
31
32
33/** Set the euid of this process to a non-root uid */
34int set_nonroot()
35{
36	struct passwd *pw;
37	setpwent();
38	/* search for the first user which is non root */
39	while((pw = getpwent()) != NULL)
40		if(strcmp(pw->pw_name, "root"))
41			break;
42	endpwent();
43	if(pw == NULL) {
44		printf("There is no other user than current and root.\n");
45		return 1;
46	}
47
48	if(setuid(pw->pw_uid) != 0) {
49		if(errno == EPERM) {
50			printf("You don't have permission to change your UID.\n");
51			return 1;
52		}
53		perror("An error occurs when calling seteuid()");
54		return 1;
55	}
56
57	printf("Testing with user '%s' (euid: %d)(uid: %d)\n",
58	       pw->pw_name, (int)geteuid(), (int)getuid());
59	return 0;
60}
61
62int main(int argc, char **argv)
63{
64
65	struct sched_param param;
66	int result = -1;
67
68	/* We assume process Number 1 is created by root */
69	/* and can only be accessed by root */
70	/* This test should be run under standard user permissions */
71	if (getuid() == 0) {
72                if (set_nonroot() != 0) {
73			printf("Cannot run this test as non-root user\n");
74			return PTS_UNTESTED;
75		}
76	}
77
78	result = sched_getparam( 1, &param);
79
80	if(result == -1 && errno == EPERM) {
81		printf("Test PASSED\n");
82		return PTS_PASS;
83	}
84	if(result == 0) {
85		printf("The function sched_getparam has successed.\n");
86		return PTS_FAIL;
87	}
88	if(errno != EPERM ) {
89		perror("errno is not EPERM: The system allows a non-root"
90			"user to use sched_getparam()");
91		return PTS_UNRESOLVED;
92	} else {
93		perror("Unresolved test error");
94		return PTS_UNRESOLVED;
95	}
96}
97
98
99