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
16 #include <pthread.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include "test.h"
21
22 static FILE *fp;
23 const char *path = "/data/test.txt";
24
do_child_one(void *a)25 void *do_child_one(void *a)
26 {
27 puts("start do_child_one");
28
29 flockfile(stdout);
30
31 puts("after first flockfile");
32
33 flockfile(stdout);
34
35 puts("foo");
36
37 funlockfile(stdout);
38
39 puts("after first funlockfile");
40
41 funlockfile(stdout);
42
43 puts("all done");
44 return a;
45 }
46
do_child_two(void *p)47 void *do_child_two(void *p)
48 {
49 char buf[64];
50
51 flockfile(fp);
52
53 if (fseek(fp, 0L, SEEK_SET) == -1) {
54 t_error("%s fseek failed\n", __func__);
55 }
56 int ret = fread(buf, 64, 1, fp);
57
58 int count = atoi(buf);
59 ++count;
60 sprintf(buf, "%d", count);
61 if (fseek(fp, 0L, SEEK_SET) == -1) {
62 t_error("%s fseek failed\n", __func__);
63 }
64 ret = fwrite(buf, strlen(buf), 1, fp);
65
66 funlockfile(fp);
67 return NULL;
68 }
69
70 /**
71 * @tc.name : flockfile_0100
72 * @tc.desc : Acquire for a thread ownership of a (FILE *) object (stdout)
73 * @tc.level : Level 0
74 */
flockfile_0100(void)75 void flockfile_0100(void)
76 {
77 pthread_t th;
78
79 if (pthread_create(&th, NULL, do_child_one, NULL) != 0) {
80 t_error("%s pthread_create failed\n", __func__);
81 }
82
83 void *result;
84 if (pthread_join(th, &result) != 0) {
85 t_error("%s pthread_join failed\n", __func__);
86 } else if (result != NULL) {
87 t_error("%s wrong return value: %p, expected %p\n", __func__, result, NULL);
88 }
89 }
90
91 /**
92 * @tc.name : flockfile_0200
93 * @tc.desc : Shared FILE* to implements mutual exclusion
94 * @tc.level : Level 1
95 */
flockfile_0200(void)96 void flockfile_0200(void)
97 {
98 pthread_t tid[100];
99
100 fp = fopen(path, "w+");
101 if (fp == NULL) {
102 t_error("%s fopen failed\n", __func__);
103 }
104
105 for (int i = 0; i < 100; i++) {
106 if (pthread_create(tid + i, NULL, do_child_two, NULL) != 0) {
107 t_error("%s pthread_create failed\n", __func__);
108 }
109 }
110
111 for (int j = 0; j < 100; j++) {
112 if (pthread_join(tid[j], NULL) != 0) {
113 t_error("%s pthread_join failed\n", __func__);
114 }
115 }
116
117 fclose(fp);
118 remove(path);
119 }
120
main(int argc, char *argv[])121 int main(int argc, char *argv[])
122 {
123 flockfile_0100();
124 flockfile_0200();
125 return t_status;
126 }