Whamcloud - gitweb
LU-8275 tests: add flag to enable secret shared key for tests
[fs/lustre-release.git] / lustre / tests / listxattr_size_check.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  *
22  * Copyright (c) 2013, Intel Corporation.
23  *
24  * lustre/tests/listxattr_size_check.c
25  *
26  * Author: Keith Mannthey <keith.mannthey@intel.com>
27  */
28
29 #include <sys/types.h>
30 #include <sys/xattr.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35
36 void usage(char *prog)
37 {
38         printf("Usage: %s <pathname>\n", prog);
39 }
40
41 /* Test the listxattr return value when the buffer is small. */
42 int main(int argc, char *argv[])
43 {
44         char *path, *buf;
45         ssize_t ret_buf, ret_null, error_s;
46         int ret = 0;
47
48         if (argc != 2) {
49                 usage(argv[0]);
50                 exit(1);
51         }
52
53         path = argv[1];
54
55         ret_null = listxattr(path, NULL, 0);
56         if (ret_null < 0) {
57                 fprintf(stderr, "listxattr(%s, NULL, 0) failed "
58                                 "with %i: %s\n", path, errno,
59                                  strerror(errno));
60                 ret = errno;
61                 goto out;
62         }
63
64         /* LU-3403 llite: error of listxattr when buffer is small */
65         if (ret_null < 2) {
66                 fprintf(stderr, "listxattr(%s, NULL, 0) returned"
67                                         "a sizes less than 2", path);
68                 ret = EINVAL;
69                 goto out;
70         }
71
72         error_s = ret_null - 1;
73         buf = (char *)malloc(error_s);
74         if (buf == NULL) {
75                 fprintf(stderr, "malloc(%zi) failed with %i: %s\n",
76                                 error_s, errno, strerror(errno));
77                 ret = errno;
78                 goto out;
79         }
80
81         ret_buf = llistxattr(path, buf, error_s);
82         if (ret_buf != -1) {
83                 fprintf(stderr, "llistxattr(%s, %p, %zi) passed with %zi but "
84                                 "should have failed with -1\n", path, buf,
85                                  error_s, ret_buf);
86                 ret = EINVAL;
87                 goto free;
88         }
89
90 free:
91         free(buf);
92 out:
93         return ret;
94 }