Various improvements. Close-on-exec and close on failure on either
[egate.git] / util / gip.c
1 /* Gateway kernel
2  * Europagate, 1995
3  *
4  * $Log: gip.c,v $
5  * Revision 1.5  1995/05/01 16:27:28  adam
6  * Various improvements. Close-on-exec and close on failure on either
7  * read or write FIFO.
8  *
9  * Revision 1.4  1995/04/21  16:38:07  adam
10  * Added more debugging logs.
11  *
12  * Revision 1.3  1995/04/20  15:12:42  adam
13  * Minor hacks really.
14  *
15  * Revision 1.2  1995/03/27  12:52:18  adam
16  * A little more verbose in marc dump.
17  *
18  * Revision 1.1  1995/03/27  08:24:58  adam
19  * New module gip: Gateway IPc module.
20  * New module gw-db: Gateway hash-db module (user information table).
21  *
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <assert.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/file.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33
34 #include <gw-log.h>
35
36 #include <gip.h>
37
38 GIP gip_initialize (const char *name)
39 {
40     GIP gip = malloc (sizeof(*gip));
41
42     if (!gip)
43         return NULL;
44     if (!(gip->name = malloc (strlen(name)+1)))
45     {
46         free (gip);
47         return NULL;
48     }
49     strcpy (gip->name, name);
50     gip->ret = mknod (gip->name, S_IFIFO|0666, 0);
51     gip->errno = errno;
52     gip->rfd = gip->wfd = -1;
53     gw_log (GW_LOG_DEBUG, "gip", "Creating %s", gip->name);
54     return gip;
55 }
56
57 int gip_destroy (GIP gip)
58 {
59     assert (gip);
60
61     free (gip->name);
62     free (gip);
63     return 0;
64 }
65
66 int gip_infileno (GIP gip)
67 {
68     return gip->rfd;
69 }
70
71 int gip_outfileno (GIP gip)
72 {
73     return gip->wfd;
74 }
75
76 int gip_errno (GIP gip)
77 {
78     return gip->errno;
79 }
80
81 int gip_read (GIP gip, char *buf, size_t count)
82 {
83     int r, no = 0;
84     while (no < count)
85     {
86         r = read (gip->rfd, buf+no, count-no);
87         if (r == -1)
88         {
89             gip->errno = errno;
90             return -1;
91         }
92         no += r;
93     }
94     return 0;
95 }
96
97 int gip_write (GIP gip, const char *buf, size_t count)
98 {
99     int r, no = 0;
100     while (no < count)
101     {
102         r = write (gip->wfd, buf+no, count-no);
103         if (r == -1)
104         {
105             gip->errno = errno;
106             return -1;
107         }
108         no += r;
109     }
110     return 0;
111 }
112
113 int gip_wline (GIP gip, const char *buf)
114 {
115     return gip_write (gip, buf, strlen(buf));
116 }
117