1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 // #define _GNU_SOURCE
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <malloc.h>
19 #include <stdio.h>
20 #include "test.h"
21
22 const char *pathname = "/proc/self/mountinfo";
23
24 /**
25 * @tc.name : name_to_handle_at_0100
26 * @tc.desc : Verify that the name_TO_HANDLE_AT function was successfully called (all parameters are valid)
27 * @tc.level : Level 0
28 */
name_to_handle_at_0100(void)29 void name_to_handle_at_0100(void)
30 {
31 struct file_handle *fhp;
32 char buf[1000];
33 int mount_id, fhsize, flags, dirfd;
34
35 fhsize = sizeof(*fhp);
36 fhp = malloc(fhsize);
37 if (fhp == NULL) {
38 t_error("%s malloc failed\n", __func__);
39 }
40
41 dirfd = AT_FDCWD;
42 flags = 0;
43 fhp->handle_bytes = 0;
44
45 if (name_to_handle_at(dirfd, pathname, fhp, &mount_id, flags) != -1 || errno != EOVERFLOW) {
46 t_error("%s Unexpected name_to_handle_at\n", __func__);
47 }
48
49 fhsize = sizeof(*fhp) + fhp->handle_bytes;
50 fhp = realloc(fhp, fhsize);
51 if (fhp == NULL) {
52 t_error("%s realloc failed\n", __func__);
53 }
54
55 int result = name_to_handle_at(dirfd, pathname, fhp, &mount_id, flags);
56 if (result == -1) {
57 t_error("%s name_to_handle_at failed\n");
58 }
59
60 int fd = open_by_handle_at(mount_id, fhp, O_RDONLY);
61 if (fd == -1) {
62 t_error("%s open_by_handle_at failed\n", __func__);
63 }
64
65 int nread = read(fd, buf, sizeof(buf));
66 if (nread == -1) {
67 t_error("%s read failed\n", __func__);
68 }
69 }
70
main(int argc,char * argv[])71 int main(int argc, char *argv[])
72 {
73 // name_to_handle_at_0100();
74 return t_status;
75 }
76