1
2/*
3 * Copyright (c) 2002, Intel Corporation.
4 * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
5 * This file is licensed under the GPL license.  For the full content
6 * of this license, see the COPYING file at the top level of this
7 * source tree.
8 */
9
10
11#include <sys/select.h>
12
13
14int sync_pipe_create(int fd[])
15{
16        return pipe (fd);
17}
18
19int sync_pipe_close(int fd[])
20{
21        int r = 0;
22
23        if (fd[0] != -1)
24                r = close (fd[0]);
25        if (fd[1] != -1)
26                r |= close (fd[1]);
27        return r;
28}
29
30int sync_pipe_wait(int fd[])
31{
32        char buf;
33        int r;
34
35        if (fd[1] != -1) {
36                close (fd[1]);
37                fd[1] = -1;
38        }
39
40        r = read (fd[0], &buf, 1);
41
42        if ((r != 1) || (buf != 'A'))
43                return -1;
44        return 0;
45}
46
47int sync_pipe_wait_select(int fd[], long tv_sec)
48{
49        int r;
50       fd_set rfds;
51       struct timeval tv;
52       int err;
53
54       tv.tv_sec = tv_sec;
55       tv.tv_usec = 0;
56
57        if (fd[1] != -1) {
58                close (fd[1]);
59                fd[1] = -1;
60        }
61
62       FD_ZERO(&rfds);
63       FD_SET(fd[0], &rfds);
64
65       r = select(fd[0] + 1, &rfds, NULL, NULL, &tv);
66       err = errno;
67
68       if (FD_ISSET(fd[0], &rfds)) {
69               return sync_pipe_wait(fd);
70       }
71
72       return r ? err : -ETIMEDOUT;
73}
74
75
76int sync_pipe_notify(int fd[])
77{
78        char buf = 'A';
79        int r;
80
81        if (fd[0] != -1) {
82                close (fd[0]);
83                fd[0] = -1;
84        }
85
86        r = write (fd[1], &buf, 1);
87
88        if (r != 1)
89                return -1;
90        return 0;
91}
92