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