1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/*++
/* NAME
/* mail_flow 3
/* SUMMARY
/* global mail flow control
/* SYNOPSIS
/* #include <mail_flow.h>
/*
/* ssize_t mail_flow_get(count)
/* ssize_t count;
/*
/* ssize_t mail_flow_put(count)
/* ssize_t count;
/*
/* ssize_t mail_flow_count()
/* DESCRIPTION
/* This module implements a simple flow control mechanism that
/* is based on tokens that are consumed by mail receiving processes
/* and that are produced by mail sending processes.
/*
/* mail_flow_get() attempts to read specified number of tokens. The
/* result is > 0 for success, < 0 for failure. In the latter case,
/* the process is expected to slow down a little.
/*
/* mail_flow_put() produces the specified number of tokens. The
/* token producing process is expected to produce new tokens
/* whenever it falls idle and no more tokens are available.
/*
/* mail_flow_count() returns the number of available tokens.
/* BUGS
/* The producer needs to wake up periodically to ensure that
/* tokens are not lost due to leakage.
/* LICENSE
/* .ad
/* .fi
/* The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/* Wietse Venema
/* IBM T.J. Watson Research
/* P.O. Box 704
/* Yorktown Heights, NY 10598, USA
/*--*/
/* System library. */
#include <sys_defs.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
/* Utility library. */
#include <msg.h>
#include <iostuff.h>
#include <warn_stat.h>
/* Global library. */
#include <mail_flow.h>
/* Master library. */
#include <master_proto.h>
#define BUFFER_SIZE 1024
/* mail_flow_get - read N tokens */
ssize_t mail_flow_get(ssize_t len)
{
const char *myname = "mail_flow_get";
char buf[BUFFER_SIZE];
struct stat st;
ssize_t count;
ssize_t n = 0;
/*
* Sanity check.
*/
if (len <= 0)
msg_panic("%s: bad length %ld", myname, (long) len);
/*
* Silence some wild claims.
*/
if (fstat(MASTER_FLOW_WRITE, &st) < 0)
msg_fatal("fstat flow pipe write descriptor: %m");
/*
* Read and discard N bytes. XXX AIX read() can return 0 when an open
* pipe is empty.
*/
for (count = len; count > 0; count -= n)
if ((n = read(MASTER_FLOW_READ, buf, count > BUFFER_SIZE ?
BUFFER_SIZE : count)) <= 0)
return (-1);
if (msg_verbose)
msg_info("%s: %ld %ld", myname, (long) len, (long) (len - count));
return (len - count);
}
/* mail_flow_put - put N tokens */
ssize_t mail_flow_put(ssize_t len)
{
const char *myname = "mail_flow_put";
char buf[BUFFER_SIZE];
ssize_t count;
ssize_t n = 0;
/*
* Sanity check.
*/
if (len <= 0)
msg_panic("%s: bad length %ld", myname, (long) len);
/*
* Write or discard N bytes.
*/
memset(buf, 0, len > BUFFER_SIZE ? BUFFER_SIZE : len);
for (count = len; count > 0; count -= n)
if ((n = write(MASTER_FLOW_WRITE, buf, count > BUFFER_SIZE ?
BUFFER_SIZE : count)) < 0)
return (-1);
if (msg_verbose)
msg_info("%s: %ld %ld", myname, (long) len, (long) (len - count));
return (len - count);
}
/* mail_flow_count - return number of available tokens */
ssize_t mail_flow_count(void)
{
const char *myname = "mail_flow_count";
ssize_t count;
if ((count = peekfd(MASTER_FLOW_READ)) < 0)
msg_warn("%s: %m", myname);
return (count);
}
|