1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2021, BELLSOFT. All rights reserved.
4 * Copyright (c) International Business Machines  Corp., 2001
5 */
6
7/*\
8 * [Description]
9 *
10 * Testcase to test whether sched_setscheduler(2) sets the errnos
11 * correctly.
12 *
13 * [Algorithm]
14 *
15 * 1. Call sched_setscheduler with an invalid pid, and expect
16 *    ESRCH to be returned.
17 * 2. Call sched_setscheduler with an invalid scheduling policy,
18 *    and expect EINVAL to be returned.
19 * 3. Call sched_setscheduler with an invalid "param" address,
20 *    which lies outside the address space of the process, and expect
21 *    EFAULT to be returned.
22 * 4. Call sched_setscheduler with an invalid priority value
23 *    in "param" and expect EINVAL to be returned
24 */
25
26#include <stdio.h>
27#include <errno.h>
28#include <pwd.h>
29
30#include "tst_test.h"
31#include "tst_sched.h"
32
33#define SCHED_INVALID	99
34
35static struct sched_param p;
36static struct sched_param p1 = { .sched_priority = 1 };
37static pid_t unused_pid;
38static pid_t init_pid = 1;
39static pid_t zero_pid;
40
41struct test_cases_t {
42	pid_t *pid;
43	int policy;
44	struct sched_param *p;
45	int error;
46} tcases[] = {
47	/* The pid is invalid - ESRCH */
48	{&unused_pid, SCHED_OTHER, &p, ESRCH},
49	/* The policy is invalid - EINVAL */
50	{&init_pid, SCHED_INVALID, &p, EINVAL},
51	/* The param address is invalid - EFAULT */
52	{&init_pid, SCHED_OTHER, (struct sched_param *)-1, EFAULT},
53	/* The priority value in param invalid - EINVAL */
54	{&zero_pid, SCHED_OTHER, &p1, EINVAL}
55};
56
57static void setup(void)
58{
59	tst_res(TINFO, "Testing %s variant", sched_variants[tst_variant].desc);
60
61	unused_pid = tst_get_unused_pid();
62}
63
64static void run(unsigned int n)
65{
66	struct sched_variant *tv = &sched_variants[tst_variant];
67	struct test_cases_t *tc = &tcases[n];
68
69	TST_EXP_FAIL(tv->sched_setscheduler(*tc->pid, tc->policy, tc->p),
70		     tc->error, "sched_setscheduler(%d, %d, %p)",
71		     *tc->pid, tc->policy, tc->p);
72}
73
74static struct tst_test test = {
75	.setup = setup,
76	.test_variants = ARRAY_SIZE(sched_variants),
77	.tcnt = ARRAY_SIZE(tcases),
78	.test = run,
79};
80