ze-filter  (ze-filter-0.8.0-develop-180218)
ze-ipc.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : janvier 2002
12  *
13  * This program is free software, but with restricted license :
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 
24 #include <ze-sys.h>
25 
26 #include "ze-libjc.h"
27 
28 #define USE_SOCKETPAIR 1
29 
30 #if (USE_SOCKETPAIR == 1) && !defined (HAVE_SOCKETPAIR)
31 #undef USE_SOCKETPAIR
32 #endif /* USE_SOCKETPAIR */
33 
34 /* ****************************************************************************
35  * *
36  * *
37  **************************************************************************** */
38 int
40  int *p;
41 {
42 #if USE_SOCKETPAIR
43  return (socketpair(AF_UNIX, SOCK_STREAM, 0, p));
44 #else
45  return (pipe(p));
46 #endif
47 }
48 
49 /* ****************************************************************************
50  * *
51  * *
52  **************************************************************************** */
53 int
55  int p[2];
56  int msg;
57  int who;
58 {
59  int val;
60  int chan;
61 
62  chan = (who == CHAN_FATHER ? 1 : 0);
63 
64  if ((val = fcntl(p[chan], F_GETFL, 0)) < 0)
65  {
66  ZE_LogSysWarning("can't get pipe status");
67  return 1;
68  }
69  val &= O_ACCMODE;
70  if ((val != O_WRONLY) && (val != O_RDWR))
71  {
72  ZE_LogMsgWarning(0, "pipe closed ?");
73  return 1;
74  }
75 
76  if (write(p[chan], &msg, sizeof (msg)) != sizeof (msg))
77  {
78  if (errno == EPIPE || ze_logLevel > 20)
79  ZE_LogSysWarning("write %d -> pipe", msg);
80  return 1;
81  }
82  return 0;
83 }
84 
85 /* ****************************************************************************
86  * *
87  * *
88  **************************************************************************** */
89 int
91  int p[2];
92  int *msg;
93  int who;
94 {
95  int val;
96  int chan;
97 
98  chan = (who == CHAN_FATHER ? 1 : 0);
99 
100  if ((val = fcntl(p[chan], F_GETFL, 0)) < 0)
101  {
102  ZE_LogSysWarning("can't get pipe status");
103  return 1;
104  }
105  val &= O_ACCMODE;
106  if (val != O_RDONLY && val != O_RDWR)
107  {
108  ZE_LogMsgWarning(0, "pipe closed ?");
109  /* return 1; */
110  }
111 
112  if (read(p[chan], msg, sizeof (*msg)) != sizeof (*msg))
113  {
114  if (ze_logLevel > 20)
115  ZE_LogSysWarning("read <- pipe");
116  return 1;
117  }
118  return 0;
119 }
120 
121 /* ****************************************************************************
122  * *
123  * *
124  **************************************************************************** */
125 bool
127  int fd;
128  int msg;
129 {
130  int val;
131 
132  if ((val = fcntl(fd, F_GETFL, 0)) < 0)
133  {
134  ZE_LogSysWarning("can't get pipe status");
135  return FALSE;
136  }
137  val &= O_ACCMODE;
138  if ((val != O_WRONLY) && (val != O_RDWR))
139  {
140  ZE_LogMsgWarning(0, "pipe closed ?");
141  return FALSE;
142  }
143 
144  if (write(fd, &msg, sizeof (msg)) != sizeof (msg))
145  {
146  if ((errno == EPIPE) || (ze_logLevel > 20))
147  ZE_LogSysWarning("write %d -> pipe", msg);
148  return FALSE;
149  }
150  return TRUE;
151 }
152 
153 /* ****************************************************************************
154  * *
155  * *
156  **************************************************************************** */
157 bool
159  int fd;
160  int *msg;
161 {
162  int val;
163 
164  if ((val = fcntl(fd, F_GETFL, 0)) < 0)
165  {
166  ZE_LogSysWarning("can't get pipe status");
167  return FALSE;
168  }
169  val &= O_ACCMODE;
170  if (val != O_RDONLY && val != O_RDWR)
171  {
172  ZE_LogMsgWarning(0, "pipe closed ?");
173  return FALSE;
174  }
175 
176  if (read(fd, msg, sizeof (*msg)) != sizeof (*msg))
177  {
178  if (ze_logLevel > 20)
179  ZE_LogSysWarning("read <- pipe");
180  return FALSE;
181  }
182  return TRUE;
183 }
int open_channel(int *p)
Definition: ze-ipc.c:39
int recv_msg_channel(p, int *msg, int who)
Definition: ze-ipc.c:90
int ze_logLevel
Definition: zeSyslog.c:34
#define FALSE
Definition: macros.h:160
#define CHAN_FATHER
Definition: ze-ipc.h:27
#define TRUE
Definition: macros.h:157
#define ZE_LogSysWarning(...)
Definition: zeSyslog.h:128
#define ZE_LogMsgWarning(level,...)
Definition: zeSyslog.h:112
bool send_message_pipe(int fd, int msg)
Definition: ze-ipc.c:126
int msg[MAX_SCORE+2]
Definition: ze-stats.c:41
int send_msg_channel(p, int msg, int who)
Definition: ze-ipc.c:54
bool recv_message_pipe(int fd, int *msg)
Definition: ze-ipc.c:158