Home | History | Annotate | Download | only in sched_setattr
      1 /*
      2  * Copyright (c) Huawei Technologies Co., Ltd., 2015
      3  * This program is free software; you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation; either version 2 of the License, or
      6  *  (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
     11  * the GNU General Public License for more details.
     12  */
     13  /* Description:
     14  *   Verify that:
     15  *              1) sched_setattr succeed with correct parameters
     16  *              2) sched_setattr fails with unused pid
     17  *              3) sched_setattr fails with invalid address
     18  *              4) sched_setattr fails with invalid flag
     19  */
     20 
     21 #define _GNU_SOURCE
     22 #include <unistd.h>
     23 #include <stdio.h>
     24 #include <stdlib.h>
     25 #include <string.h>
     26 #include <time.h>
     27 #include <linux/unistd.h>
     28 #include <linux/kernel.h>
     29 #include <linux/types.h>
     30 #include <sys/syscall.h>
     31 #include <pthread.h>
     32 #include <sched.h>
     33 #include <errno.h>
     34 
     35 #include "test.h"
     36 #include "lapi/sched.h"
     37 
     38 char *TCID = "sched_setattr01";
     39 
     40 #define SCHED_DEADLINE	6
     41 #define RUNTIME_VAL 10000000
     42 #define PERIOD_VAL 30000000
     43 #define DEADLINE_VAL 30000000
     44 
     45 static pid_t pid;
     46 static pid_t unused_pid;
     47 
     48 static struct sched_attr attr = {
     49 	.size = sizeof(struct sched_attr),
     50 	.sched_flags = 0,
     51 	.sched_nice = 0,
     52 	.sched_priority = 0,
     53 
     54 	.sched_policy = SCHED_DEADLINE,
     55 	.sched_runtime = RUNTIME_VAL,
     56 	.sched_period = PERIOD_VAL,
     57 	.sched_deadline = DEADLINE_VAL,
     58 };
     59 
     60 static struct test_case {
     61 	pid_t *pid;
     62 	struct sched_attr *a;
     63 	unsigned int flags;
     64 	int exp_return;
     65 	int exp_errno;
     66 } test_cases[] = {
     67 	{&pid, &attr, 0, 0, 0},
     68 	{&unused_pid, &attr, 0, -1, ESRCH},
     69 	{&pid, NULL, 0, -1, EINVAL},
     70 	{&pid, &attr, 1000, -1, EINVAL}
     71 };
     72 
     73 static void setup(void);
     74 static void sched_setattr_verify(const struct test_case *test);
     75 
     76 int TST_TOTAL = ARRAY_SIZE(test_cases);
     77 
     78 void *do_test(void *data LTP_ATTRIBUTE_UNUSED)
     79 {
     80 	int i;
     81 
     82 	for (i = 0; i < TST_TOTAL; i++)
     83 		sched_setattr_verify(&test_cases[i]);
     84 
     85 	return NULL;
     86 }
     87 
     88 static void sched_setattr_verify(const struct test_case *test)
     89 {
     90 	TEST(sched_setattr(*(test->pid), test->a, test->flags));
     91 
     92 	if (TEST_RETURN != test->exp_return) {
     93 		tst_resm(TFAIL | TTERRNO, "sched_setattr(%i,attr,%u) "
     94 		         "returned: %ld expected: %d",
     95 		         *(test->pid), test->flags,
     96 		         TEST_RETURN, test->exp_return);
     97 		return;
     98 	}
     99 
    100 	if (TEST_ERRNO == test->exp_errno) {
    101 		tst_resm(TPASS | TTERRNO,
    102 			"sched_setattr() works as expected");
    103 		return;
    104 	}
    105 
    106 	tst_resm(TFAIL | TTERRNO, "sched_setattr(%i,attr,%u): "
    107 		"expected: %d - %s",
    108 		*(test->pid), test->flags,
    109 		test->exp_errno, tst_strerrno(test->exp_errno));
    110 }
    111 
    112 int main(int argc, char **argv)
    113 {
    114 	pthread_t thread;
    115 	int lc;
    116 
    117 	tst_parse_opts(argc, argv, NULL, NULL);
    118 
    119 	setup();
    120 
    121 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    122 		pthread_create(&thread, NULL, do_test, NULL);
    123 		pthread_join(thread, NULL);
    124 	}
    125 
    126 	tst_exit();
    127 }
    128 
    129 void setup(void)
    130 {
    131 	unused_pid = tst_get_unused_pid(setup);
    132 
    133 	tst_require_root();
    134 
    135 	if ((tst_kvercmp(3, 14, 0)) < 0)
    136 		tst_brkm(TCONF, NULL, "EDF needs kernel 3.14 or higher");
    137 
    138 	TEST_PAUSE;
    139 }
    140