cb0b697d02880d24d4f763a4829f7ca54a317d6e
[egate.git] / util / gipc.c
1 /* Gateway kernel
2  * Europagate, 1995
3  *
4  * $Log: gipc.c,v $
5  * Revision 1.6  1995/05/01 16:27:29  adam
6  * Various improvements. Close-on-exec and close on failure on either
7  * read or write FIFO.
8  *
9  * Revision 1.5  1995/04/21  16:38:07  adam
10  * Added more debugging logs.
11  *
12  * Revision 1.4  1995/04/20  15:12:45  adam
13  * Minor hacks really.
14  *
15  * Revision 1.3  1995/04/19  16:02:06  adam
16  * Some hacks to get the FIFO communication work!! Isn't reliable.
17  *
18  * Revision 1.2  1995/03/28  08:03:46  adam
19  * Non-blocking open used when sync is set.
20  *
21  * Revision 1.1  1995/03/27  08:24:59  adam
22  * New module gip: Gateway IPc module.
23  * New module gw-db: Gateway hash-db module (user information table).
24  *
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33
34 #include <gw-log.h>
35 #include <gip.h>
36
37 GIP gipc_initialize (const char *name)
38 {
39     return gip_initialize (name);
40 }
41
42 int gipc_destroy (GIP gip)
43 {
44     return gip_destroy (gip);
45 }
46
47 int gipc_open (GIP gip, const char *server, int sync)
48 {
49     if (sync) 
50     {
51         gw_log (GW_LOG_DEBUG, "gipc", "Open sync writeonly of %s", server);
52         gip->wfd = open (server, O_WRONLY);
53         gw_log (GW_LOG_DEBUG, "gipc", "got wfd %d", gip->wfd);
54
55         gw_log (GW_LOG_DEBUG, "gipc", "Open sync readonly of %s", gip->name);
56         gip->rfd = open (gip->name, O_RDONLY);
57         gw_log (GW_LOG_DEBUG, "gipc", "got rfd %d", gip->rfd);
58     }
59     else
60     {
61         gw_log (GW_LOG_DEBUG, "gipc", "Open readonly of %s", gip->name);
62         gip->rfd = open (gip->name, O_RDONLY|O_NONBLOCK);
63         gw_log (GW_LOG_DEBUG, "gipc", "got rfd %d", gip->rfd);
64
65         gw_log (GW_LOG_DEBUG, "gipc", "Open witeonly of %s", server);
66         gip->wfd = open (server, O_WRONLY|O_NONBLOCK);
67         gw_log (GW_LOG_DEBUG, "gipc", "got wfd %d", gip->wfd);
68     }
69     if (gip->rfd == -1)
70     {
71         gw_log (GW_LOG_DEBUG, "gipc", "Cannot open %s", gip->name);
72         if (gip->wfd != -1)
73         {
74             close (gip->wfd);
75             gip->wfd = -1;
76         }
77         return -1;
78     }
79     if (gip->wfd == -1)
80     {
81         gw_log (GW_LOG_DEBUG, "gipc", "Cannot open %s", server);
82         close (gip->rfd);
83         gip->rfd = -1;
84         return -2;
85     }
86     fcntl (gip->wfd, F_SETFL, 0);
87     fcntl (gip->rfd, F_SETFL, 0);
88     fcntl (gip->wfd, F_SETFD, FD_CLOEXEC);
89     fcntl (gip->rfd, F_SETFD, FD_CLOEXEC);
90     return 0;
91 }
92
93 int gipc_close (GIP gip)
94 {
95     if (gip->rfd != -1)
96     {
97         close (gip->rfd);
98         gip->rfd = -1;
99     }
100     if (gip->wfd != -1)
101     {
102         close (gip->wfd);
103         gip->wfd = -1;
104     }
105     return 0;
106 }