• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fcntl.h>
17 #include <stdlib.h>
18 #include <sys/stat.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <termios.h>
22 #include <time.h>
23 #include "test.h"
24 
25 /**
26  * @tc.name      : tcdrain_0100
27  * @tc.desc      : Wait until output has been transmitted
28  * @tc.level     : Level 0
29  */
tcdrain_0100(void)30 void tcdrain_0100(void)
31 {
32     char Master[] = "/dev/ptmx";
33     char text[] = "text to be written to tty";
34     char data[80];
35     int fd_master, fd_slave, result;
36     time_t T;
37 
38     fd_master = open(Master, O_RDWR | O_NONBLOCK);
39     if (fd_master < 0) {
40         t_error("%s open master failed", __func__);
41     }
42 
43     result = grantpt(fd_master);
44     if (result < 0) {
45         t_error("%s grantpt failed", __func__);
46         return;
47     }
48 
49     result = unlockpt(fd_master);
50     if (result < 0) {
51         t_error("%s unlockpt failed", __func__);
52         return;
53     }
54 
55     char *Slave = ptsname(fd_master);
56     fd_slave = open(Slave, O_RDWR | O_NONBLOCK);
57     if (fd_slave < 0) {
58         t_error("%s open slave failed", __func__);
59     }
60 
61     if (fork() == 0) {
62         if (write(fd_slave, text, strlen(text) + 1) == -1) {
63             t_error("%s write failed", __func__);
64         }
65         time(&T);
66         printf("child has written to tty, tcdrain() started at %s", ctime(&T));
67 
68         if (tcdrain(fd_slave) != 0) {
69             t_error("%s tcdrain failed", __func__);
70         }
71 
72         time(&T);
73         printf("tcdrain() returned at %s", ctime(&T));
74         exit(0);
75     }
76 
77     time(&T);
78     printf("parent is starting nap at %s", ctime(&T));
79     sleep(2);
80     time(&T);
81     printf("parent is done with nap at %s", ctime(&T));
82     if (read(fd_master, data, sizeof(data)) == -1) {
83         t_error("%s read failed", __func__);
84     } else {
85         printf("read '%s' from the tty\n", data);
86     }
87     sleep(2);
88     close(fd_slave);
89     close(fd_master);
90 }
91 
main(int argc,char * argv[])92 int main(int argc, char *argv[])
93 {
94     tcdrain_0100();
95 
96     return t_status;
97 }