Home | History | Annotate | Download | only in sched_getattr
      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 
     14  /* Description:
     15  *   Verify that:
     16  *              1) sched_getattr fails with unused pid
     17  *              2) sched_getattr fails with invalid address
     18  *              3) sched_getattr fails with invalid value
     19  *              4) sched_getattr fails with invalid flag
     20  */
     21 
     22 #define _GNU_SOURCE
     23 
     24 #include <unistd.h>
     25 #include <stdio.h>
     26 #include <stdlib.h>
     27 #include <string.h>
     28 #include <time.h>
     29 #include <linux/unistd.h>
     30 #include <linux/kernel.h>
     31 #include <linux/types.h>
     32 #include <sys/syscall.h>
     33 #include <pthread.h>
     34 #include <sched.h>
     35 #include <errno.h>
     36 
     37 #include "test.h"
     38 #include "lapi/syscalls.h"
     39 #include "lapi/sched.h"
     40 
     41 char *TCID = "sched_getattr02";
     42 
     43 static pid_t pid;
     44 static pid_t unused_pid;
     45 struct sched_attr attr_copy;
     46 
     47 static struct test_case {
     48 	pid_t *pid;
     49 	struct sched_attr *a;
     50 	unsigned int size;
     51 	unsigned int flags;
     52 	int exp_errno;
     53 } test_cases[] = {
     54 	{&unused_pid, &attr_copy, sizeof(struct sched_attr), 0, ESRCH},
     55 	{&pid, NULL, sizeof(struct sched_attr), 0, EINVAL},
     56 	{&pid, &attr_copy, sizeof(struct sched_attr) - 1, 0, EINVAL},
     57 	{&pid, &attr_copy, sizeof(struct sched_attr), 1000, EINVAL}
     58 };
     59 
     60 static void setup(void);
     61 static void sched_getattr_verify(const struct test_case *test);
     62 
     63 int TST_TOTAL = ARRAY_SIZE(test_cases);
     64 
     65 static void sched_getattr_verify(const struct test_case *test)
     66 {
     67 	TEST(sched_getattr(*(test->pid), test->a, test->size,
     68 			test->flags));
     69 
     70 	if (TEST_RETURN != -1) {
     71 		tst_resm(TFAIL, "sched_getattr() succeeded unexpectedly.");
     72 		return;
     73 	}
     74 
     75 	if (TEST_ERRNO == test->exp_errno) {
     76 		tst_resm(TPASS | TTERRNO,
     77 			"sched_getattr() failed expectedly");
     78 		return;
     79 	}
     80 
     81 	tst_resm(TFAIL | TTERRNO, "sched_getattr() failed unexpectedly "
     82 		": expected: %d - %s",
     83 		test->exp_errno, tst_strerrno(test->exp_errno));
     84 }
     85 
     86 int main(int argc, char **argv)
     87 {
     88 	int lc, i;
     89 
     90 	tst_parse_opts(argc, argv, NULL, NULL);
     91 
     92 	setup();
     93 
     94 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     95 		for (i = 0; i < TST_TOTAL; i++)
     96 			sched_getattr_verify(&test_cases[i]);
     97 	}
     98 
     99 	tst_exit();
    100 }
    101 
    102 void setup(void)
    103 {
    104 	unused_pid = tst_get_unused_pid(setup);
    105 
    106 	if ((tst_kvercmp(3, 14, 0)) < 0)
    107 		tst_brkm(TCONF, NULL, "EDF needs kernel 3.14 or higher");
    108 
    109 	TEST_PAUSE;
    110 }
    111