1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <mntent.h>
17570af302Sopenharmony_ci#include <pthread.h>
18570af302Sopenharmony_ci#include <stdbool.h>
19570af302Sopenharmony_ci#include "functionalext.h"
20570af302Sopenharmony_ci
21570af302Sopenharmony_cistatic pthread_barrier_t g_barrier;
22570af302Sopenharmony_ci#define WAIT() pthread_barrier_wait(&g_barrier)
23570af302Sopenharmony_ci
24570af302Sopenharmony_civoid *getmntent_rOne()
25570af302Sopenharmony_ci{
26570af302Sopenharmony_ci    char c[1000];
27570af302Sopenharmony_ci    char str[100];
28570af302Sopenharmony_ci    FILE *fptr = NULL;
29570af302Sopenharmony_ci    char strings[1024];
30570af302Sopenharmony_ci    if ((fptr = fopen("/proc/mounts", "r")) == NULL) {
31570af302Sopenharmony_ci        t_error("%s Error! fopen failed\n", __func__);
32570af302Sopenharmony_ci    }
33570af302Sopenharmony_ci    fscanf(fptr, "%[^\n]", c);
34570af302Sopenharmony_ci    fclose(fptr);
35570af302Sopenharmony_ci    sscanf(c, "%[^ ]", str);
36570af302Sopenharmony_ci    struct mntent *m = NULL;
37570af302Sopenharmony_ci    struct mntent mntbuf;
38570af302Sopenharmony_ci    memset(&m, 0x00, sizeof(m));
39570af302Sopenharmony_ci    memset(&strings[0], 0x00, sizeof(strings));
40570af302Sopenharmony_ci    FILE *ffp = setmntent("/proc/mounts", "r");
41570af302Sopenharmony_ci    EXPECT_PTRNE("getmntent_r_0100", ffp, NULL);
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci    WAIT();
44570af302Sopenharmony_ci    m = getmntent_r(ffp, &mntbuf, strings, sizeof(strings));
45570af302Sopenharmony_ci    EXPECT_TRUE("getmntent_r_0100", m != NULL);
46570af302Sopenharmony_ci    EXPECT_EQ("getmntent_r_0100", strcmp(m->mnt_fsname, str), 0);
47570af302Sopenharmony_ci    EXPECT_EQ("getmntent_r_0100", strcmp(mntbuf.mnt_fsname, str), 0);
48570af302Sopenharmony_ci
49570af302Sopenharmony_ci    endmntent(ffp);
50570af302Sopenharmony_ci    return m;
51570af302Sopenharmony_ci}
52570af302Sopenharmony_ci
53570af302Sopenharmony_ci/**
54570af302Sopenharmony_ci * @tc.name      : getmntent_r_0100
55570af302Sopenharmony_ci * @tc.desc      : Multiple threads simultaneously read file information.
56570af302Sopenharmony_ci * @tc.level     : Level 0
57570af302Sopenharmony_ci */
58570af302Sopenharmony_civoid getmntent_r_0100(void)
59570af302Sopenharmony_ci{
60570af302Sopenharmony_ci    pthread_t srv;
61570af302Sopenharmony_ci    pthread_t cli;
62570af302Sopenharmony_ci    int ret = pthread_barrier_init(&g_barrier, 0, 2);
63570af302Sopenharmony_ci    EXPECT_EQ("getmntent_r_0100", 0, ret);
64570af302Sopenharmony_ci    ret = pthread_create(&srv, NULL, getmntent_rOne, NULL);
65570af302Sopenharmony_ci    EXPECT_EQ("getmntent_r_0100", 0, ret);
66570af302Sopenharmony_ci    ret = pthread_create(&cli, NULL, getmntent_rOne, NULL);
67570af302Sopenharmony_ci    EXPECT_EQ("getmntent_r_0100", 0, ret);
68570af302Sopenharmony_ci    ret = pthread_join(cli, NULL);
69570af302Sopenharmony_ci    EXPECT_EQ("getmntent_r_0100", 0, ret);
70570af302Sopenharmony_ci    ret = pthread_join(srv, NULL);
71570af302Sopenharmony_ci    EXPECT_EQ("getmntent_r_0100", 0, ret);
72570af302Sopenharmony_ci    ret = pthread_barrier_destroy(&g_barrier);
73570af302Sopenharmony_ci    EXPECT_EQ("getmntent_r_0100", 0, ret);
74570af302Sopenharmony_ci}
75570af302Sopenharmony_ci
76570af302Sopenharmony_ci/**
77570af302Sopenharmony_ci * @tc.name      : getmntent_r_0200
78570af302Sopenharmony_ci * @tc.desc      : The f parameter is invalid, reading the file information failed.
79570af302Sopenharmony_ci * @tc.level     : Level 2
80570af302Sopenharmony_ci */
81570af302Sopenharmony_civoid getmntent_r_0200(void)
82570af302Sopenharmony_ci{
83570af302Sopenharmony_ci    char c[1000];
84570af302Sopenharmony_ci    char str[100];
85570af302Sopenharmony_ci    char strings[1024];
86570af302Sopenharmony_ci
87570af302Sopenharmony_ci    FILE *fptr = fopen("/data/getmntent.txt", "w");
88570af302Sopenharmony_ci    EXPECT_TRUE("getmntent_r_0200", fptr != NULL);
89570af302Sopenharmony_ci    fclose(fptr);
90570af302Sopenharmony_ci
91570af302Sopenharmony_ci    struct mntent *m = NULL;
92570af302Sopenharmony_ci    struct mntent mntbuf;
93570af302Sopenharmony_ci    memset(&m, 0x00, sizeof(m));
94570af302Sopenharmony_ci    memset(&strings[0], 0x00, sizeof(strings));
95570af302Sopenharmony_ci    FILE *ffp = setmntent("/data/getmntent.txt", "r");
96570af302Sopenharmony_ci    EXPECT_PTRNE("getmntent_r_0200", ffp, NULL);
97570af302Sopenharmony_ci    m = getmntent_r(ffp, &mntbuf, strings, sizeof(strings));
98570af302Sopenharmony_ci    EXPECT_EQ("getmntent_r_0200", m, NULL);
99570af302Sopenharmony_ci    endmntent(ffp);
100570af302Sopenharmony_ci    remove("/data/getmntent.txt");
101570af302Sopenharmony_ci}
102570af302Sopenharmony_ci
103570af302Sopenharmony_ciint main(int argc, char *argv[])
104570af302Sopenharmony_ci{
105570af302Sopenharmony_ci    getmntent_r_0100();
106570af302Sopenharmony_ci    getmntent_r_0200();
107570af302Sopenharmony_ci
108570af302Sopenharmony_ci    return t_status;
109570af302Sopenharmony_ci}