-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarrier.cc
210 lines (178 loc) · 4.86 KB
/
barrier.cc
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (c) 2011, Adrian Thurston <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsnp.h"
#include "encrypt.h"
#include "string.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
#define MB (1024*1024)
#define KB (1024)
#define READSZ 1024
#define CTRLSZ 1024
int transmitFd( int channelFd, char cmd, int fd )
{
/* The socket message. */
iovec iov;
iov.iov_base = (void*) &cmd;
iov.iov_len = 1;
msghdr msg;
memset( &msg, 0, sizeof(msg) );
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
/* Big enough? */
char control[CTRLSZ];
msg.msg_control = control;
msg.msg_controllen = sizeof(control);
/* Attach the fd. */
cmsghdr *cmsg = CMSG_FIRSTHDR( &msg );
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN( sizeof(int) );
*(int *)CMSG_DATA(cmsg) = fd;
msg.msg_controllen = cmsg->cmsg_len;
/* Send the message with fd. */
if ( sendmsg( channelFd, &msg, 0 ) < 0 ) {
error( "fd transmit failed on sendmsg: %s\n", strerror(errno) );
return -1;
}
return 0;
}
int extractFd( int channelFd, msghdr *msg )
{
int fd = -1;
cmsghdr *cmsg = CMSG_FIRSTHDR( msg );
while ( cmsg != NULL ) {
if ( cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS ) {
/* This is an FD transfer. */
fd = *(int *) CMSG_DATA( cmsg );
break;
}
cmsg = CMSG_NXTHDR( msg, cmsg );
}
debug( DBG_PROC, "extraced fd %d\n", fd );
return fd;
}
void Barrier::command( char cmd )
{
int result = write( fd, &cmd, 1 );
if ( result == 0 )
fatal( "barrier command write: nothing written (0)\n" );
if ( result < 0 )
fatal( "barrier command write: %s\n", strerror(errno) );
}
char Barrier::readCommand()
{
char cmd;
int result = read( fd, &cmd, 1 );
if ( result == 0 ) {
debug( DBG_PROC, "received EOF on fd %d, returning BA_QUIT\n", fd );
return BA_QUIT;
}
if ( result < 1 )
fatal( "failed reading command on fd %d: %s\n", fd, strerror(errno) );
return cmd;
}
void Barrier::quit()
{
debug( DBG_PROC, "sending quit command\n" );
command( BA_QUIT );
readChar();
}
void Barrier::writeChar( char c )
{
int result = write( fd, &c, 1 );
if ( result < 1 )
fatal( "failed writing character to fd %d: %s\n", fd, strerror(errno) );
}
char Barrier::readChar()
{
char c = INVALID_THING;
int result = read( fd, &c, 1 );
if ( result < 0 ) {
fatal( "barrier failed reading character on fd %d: %s\n",
fd, strerror(errno) );
}
else if ( result < 1 ) {
error( "barrier received EOF on fd %d\n", fd );
}
return c;
}
void Barrier::writeData( const String &s )
{
int result, length = s.length;
int writeLen = sizeof(int);
result = write( fd, &length, writeLen );
if ( result < writeLen )
fatal( "barrier: write data failed: %s", strerror(errno) );
if ( length > 0 ) {
result = write( fd, s.data, s.length );
if ( result < s.length )
fatal( "barrier: write data failed: %s", strerror(errno) );
}
}
void Barrier::readData( String &s )
{
int length = 0, readLen = sizeof(int);
int result = read( fd, &length, readLen );
if ( result < readLen || length < 0 || length >= 1 * MB )
fatal( "barrier: invalid message length\n" );
if ( length > 0 ) {
s.allocate( length );
result = read( fd, s.data, s.length );
if ( result < s.length )
fatal( "barrier: failed to read data body\n" );
}
}
void Barrier::writeInt( int i )
{
int writeLen = sizeof(int);
int result = write( fd, &i, writeLen );
if ( result < writeLen )
fatal( "barrier: failed to write int\n" );
}
int Barrier::readInt()
{
int readLen = sizeof(int);
int i, result = read( fd, &i, readLen );
if ( result < readLen )
fatal( "barrier: failed to read int\n" );
return i;
}
void Barrier::writeLongLong( long long ll )
{
int writeLen = sizeof(long long);
int result = write( fd, &ll, writeLen );
if ( result < writeLen )
fatal( "barrier: failed to write long long\n" );
}
long long Barrier::readLongLong()
{
int readLen = sizeof(long long);
long long ll;
int result = read( fd, &ll, readLen );
if ( result < readLen )
fatal( "barrier: failed to read long long\n" );
return ll;
}