1 /*
2 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 */
17
18 /*
19 * Test whether ustat(2) system call returns appropriate error number for
20 * invalid dev_t parameter and for bad address paramater.
21 */
22
23 #include <unistd.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27
28 #include "config.h"
29 #include "test.h"
30 #include "safe_macros.h"
31
32 char *TCID = "ustat02";
33
34 #ifdef HAVE_USTAT
35 # ifdef HAVE_SYS_USTAT_H
36 # include <sys/ustat.h>
37 # endif
38
39 static void setup(void);
40
41 static dev_t invalid_dev = -1;
42 static dev_t root_dev;
43 struct ustat ubuf;
44
45 static struct test_case_t {
46 char *err_desc;
47 int exp_errno;
48 char *exp_errval;
49 dev_t *dev;
50 struct ustat *buf;
51 } tc[] = {
52 {"Invalid parameter", EINVAL, "EINVAL", &invalid_dev, &ubuf},
53 #ifndef UCLINUX
54 {"Bad address", EFAULT, "EFAULT", &root_dev, (void*)-1}
55 #endif
56 };
57
58 int TST_TOTAL = ARRAY_SIZE(tc);
59
main(int ac,char ** av)60 int main(int ac, char **av)
61 {
62
63 int lc, i;
64
65 tst_parse_opts(ac, av, NULL, NULL);
66
67 setup();
68
69 for (lc = 0; TEST_LOOPING(lc); lc++) {
70 tst_count = 0;
71
72 for (i = 0; i < TST_TOTAL; i++) {
73 TEST(ustat(*tc[i].dev, tc[i].buf));
74
75 if (TEST_RETURN == -1 && TEST_ERRNO == ENOSYS)
76 tst_brkm(TCONF, NULL, "ustat not supported");
77
78 if ((TEST_RETURN == -1)
79 && (TEST_ERRNO == tc[i].exp_errno)) {
80 tst_resm(TPASS,
81 "ustat(2) expected failure;"
82 " Got errno - %s : %s",
83 tc[i].exp_errval, tc[i].err_desc);
84 } else {
85 tst_resm(TFAIL | TTERRNO,
86 "ustat(2) failed to produce"
87 " expected error; %d, errno"
88 ": %s",
89 tc[i].exp_errno, tc[i].exp_errval);
90 }
91 }
92 }
93
94 tst_exit();
95 }
96
setup(void)97 static void setup(void)
98 {
99 struct stat buf;
100
101 tst_sig(NOFORK, DEF_HANDLER, NULL);
102
103 TEST_PAUSE;
104
105 /* Find a valid device number */
106 SAFE_STAT(NULL, "/", &buf);
107
108 root_dev = buf.st_dev;
109 }
110 #else
main(void)111 int main(void)
112 {
113 tst_brkm(TCONF, NULL, "system doesn't have ustat() support");
114 }
115 #endif
116