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