-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_test.c
662 lines (603 loc) · 19.6 KB
/
pipe_test.c
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/** MODIFIED **/
/****************************************************************
** **
** Copyright (c) 1996 - 2001 Caldera International, Inc. **
** All Rights Reserved. **
** **
** This program is free software; you can redistribute it **
** and/or modify it under the terms of the GNU General Public **
** License as published by the Free Software Foundation; **
** either version 2 of the License, or (at your option) any **
** later version. **
** **
** This program is distributed in the hope that it will be **
** useful, but WITHOUT ANY WARRANTY; without even the implied **
** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR **
** PURPOSE. See the GNU General Public License for more **
** details. **
** **
** You should have received a copy of the GNU General Public **
** License along with this program; if not, write to the Free **
** Software Foundation, Inc., 59 Temple Place, Suite 330, **
** Boston, MA 02111-1307 USA **
** **
****************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#ifdef BSDMEMMAP
#include <sys/mman.h>
#else
#include <sys/sem.h>
#endif
#include <sys/stat.h>
#include <sys/errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <memory.h>
#include "aim7.h"
#include <stdint.h> /** for uintprt_t **/
#define SHMKEY ((key_t)getpid())
#define SEMKEY ((key_t)getpid())
#define MAXBLK (2048)
#define dump_socket(a,b)
#define FN1 "AIM_file.XXXXXXXXXX" /* changed from 6X to 10X 10/17/95 1.1 */
#define SHMFILE "AIM_SHM.XXXXXXXXXX" /* file for shared memory for BSD */
#define FATAL(a) {fprintf(stderr,"\nFatal error %d at line %d of file %s: %s -- ",errno,__LINE__,__FILE__,a); perror(""); return(-1); }
#define FATAL_SHARED_MEM(a) {fprintf(stderr,"\nFatal error %d at line %d of file %s: %s -- ",errno,__LINE__,__FILE__,a); perror(""); \
clear_ipc(); /* free the semaphore */ \
return(-1); }
/*
* Forward Declarations
*/
static int read_write_close(int loops, char *title, int rd_fd, int wr_fd); /* the actual test */
static int count = 0;
static int sizes[] = {
1, 3, 5, 7, 16, 32, 64, 512, 1024, 2048, /* misc. sizes */
1, 3, 5, 7, 16, 32, 64, 512, 1024, 2048,
32, 32, 32, 32, 32, 32, /* x windows mostly... */
512, 512, 512, 512, 512, /* DBMS's mostly */
};
static int sem = -1, /* semaphore id */
shmid = -1; /* shared memory id */
static char *bufptr = NULL; /* pointer to message buffer in shared memory */
static double xy[100][2]; /* holds xy pairs */
static int flag = 0; /* completion flag */
static struct sockaddr_in rd_in, /* read socket */
wr_in; /* write socket */
static struct hostent *hp;
static char myname[1024]; /* hostname placed here */
COUNT_START static FILE * plot; /* plot output goes here */
union semun { /* semaphore union */
int val;
struct semid_ds *buf;
ushort *array;
} semarg; /* argument to semctl */
extern void aim_mktemp();
void init_pipe_test_c() {
int status;
extern int gethostname();
/*
* Do initialization
*/
status = gethostname(myname, sizeof (myname)); /* get machine name */
if (status < 0) { /* handle errors */
fprintf(stderr, "pipe_test_c(): couldn't gethostname()\n");
exit(-1);
}
hp = gethostbyname(myname); /* what is my address? */
if (hp == NULL) { /* handle errors */
fprintf(stderr,
"pipe_test_c(): couldn't gethostbyname() using <%s>\n",
myname);
exit(-1);
}
}
int tcp_test() {
int status, wr, rd, length, result, new;
/*
* Step 1: create write socket and everything
*/
status = wr = socket(AF_INET, SOCK_STREAM, 0); /* create write socket */
if (status < 0)
FATAL("Creating write stream socket"); /* error test */
/*
* Step 2: initialize socket structure and then bind it
*/
memset(&wr_in, 0, sizeof (wr_in)); /* clear it to zeros */
wr_in.sin_family = AF_INET; /* set family of socket */
memcpy((void *)&wr_in.sin_addr.s_addr, (void *)hp->h_addr, hp->h_length); /* ignore addresses */
wr_in.sin_port = 0; /* set write port (make kernel choose) */
status = bind(wr, (struct sockaddr *)&wr_in, sizeof (wr_in)); /* do the bind */
if (status < 0)
FATAL("bind on write"); /* error test */
/*
* Step 3: update info
*/
length = sizeof (wr_in); /* get length */
status = getsockname(wr, (struct sockaddr *)&wr_in, &length); /* get socket info */
if (status < 0)
FATAL("getsockname()"); /* if error */
/*
* tell it not to delay sending data
*/
#ifdef TCP_NODELAY
result = 1; /* enable the option */
status = setsockopt(wr, IPPROTO_TCP, TCP_NODELAY, (char *)&result, sizeof (result)); /* set it */
if (status < 0)
FATAL("setsockopt()"); /* die here if error */
length = sizeof (result); /* establish size */
result = 0; /* clear the result */
status = getsockopt(wr, IPPROTO_TCP, TCP_NODELAY, (char *)&result, &length); /* get it */
if (status < 0)
FATAL("getsockopt()"); /* die here if error */
#endif
/*
* Step 4: Allow connections
*/
listen(wr, 5); /* listen for connections */
/*
* Step 5: create read socket
*/
status = rd = socket(AF_INET, SOCK_STREAM, 0); /* create read socket */
if (status < 0)
FATAL("Creating read stream socket"); /* error check */
/*
* Step 6: connect to write socket
*/
dump_socket("Write just before connect()", wr_in);
status = connect(rd, (struct sockaddr *)&wr_in, sizeof (wr_in)); /* do the connect */
if (status < 0)
FATAL("Connect failure"); /* error check */
/*
* Step 7: allow connections
*/
/*
* new = status = accept(wr, 0, 0);
*//*
* do the accept
*/
/*
* if (status == -1) FATAL("accept");
*//*
* error check
*/
retry: /* add retry 10/17/95 1.1 */
new = status = accept(wr, 0, 0); /* do the accept */
if (status == -1 && errno == EINTR)
goto retry; /* error check */
/*
* Step 8: update information
*/
length = sizeof (wr_in); /* get length */
status = getsockname(rd, (struct sockaddr *)&rd_in, &length); /* get socket info */
if (status < 0)
FATAL("getsockname()"); /* check for error */
/*
* Step 9: go for it
*/
status = read_write_close(TCP_TEST_REP, "TCP/IP", rd, new); /* do the test */
if (status < 0)
FATAL("read_write_close()"); /* check for error */
COUNT_END("tcp_test");
close(wr); /* add 10/17/95 1.1 */
return 0;
}
int udp_test() {
int status, wr, rd, length;
/*
* Step 1: create write socket and everything
*/
status = wr = socket(AF_INET, SOCK_DGRAM, 0); /* create write socket */
if (status < 0)
FATAL("Creating write datagram socket"); /* error test */
/*
* Step 2: initialize socket structure and then bind it
*/
memset(&wr_in, 0, sizeof (wr_in)); /* clear it to zeros */
wr_in.sin_family = AF_INET; /* set family of socket */
memcpy((void *)&wr_in.sin_addr.s_addr, (void *)hp->h_addr, hp->h_length); /* ignore addresses */
wr_in.sin_port = 0; /* set write port (make kernel choose) */
status = bind(wr, (struct sockaddr *)&wr_in, sizeof (wr_in)); /* do the bind */
if (status < 0)
FATAL("bind on write"); /* error test */
/*
* Step 3: update info
*/
length = sizeof (wr_in); /* get length */
status = getsockname(wr, (struct sockaddr *)&wr_in, &length); /* get socket info */
if (status < 0)
FATAL("getsockname()"); /* if error */
/*
* Step 4: create read socket
*/
status = rd = socket(AF_INET, SOCK_DGRAM, 0); /* create read socket */
if (status < 0)
FATAL("Creating read datagram socket"); /* error check */
/*
* Step 5: initialize socket structure and bind it
*/
memset(&rd_in, 0, sizeof (rd_in)); /* clear it to zeros */
rd_in.sin_family = AF_INET; /* set family of socket */
memcpy((void *)&rd_in.sin_addr.s_addr, (void *)hp->h_addr, hp->h_length); /* ignore addresses */
rd_in.sin_port = 0; /* set write port (make kernel choose) */
status = bind(rd, (struct sockaddr *)&rd_in, sizeof (rd_in)); /* do the bind */
if (status < 0)
FATAL("bind on write"); /* error test */
/*
* Step 6: update info
*/
length = sizeof (rd_in); /* get length */
status = getsockname(rd, (struct sockaddr *)&rd_in, &length); /* get socket info */
if (status < 0)
FATAL("getsockname()"); /* if error */
/*
* Step 7: Connect ports
*/
status = connect(wr, (struct sockaddr *)&rd_in, sizeof (rd_in)); /* writes go to the read socket */
if (status < 0)
FATAL("connect()"); /* if error */
/*
* Step 8: go for it
*/
status = read_write_close(UDP_TEST_REP, "UDP/IP", rd, wr); /* do the test */
if (status < 0)
FATAL("read_write_close()"); /* check for error */
COUNT_END("udp_test");
return 0;
}
/**********************************************************
* Stream Pipes
**********************************************************/
int stream_pipe() {
int status, pipefd[2];
#ifndef NO_SOCKETPAIR
status = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd); /* create stream sockets */
#endif
if (status < 0)
FATAL("Cannot create stream pipes"); /* if error, stop */
status = read_write_close(STREAM_PIPE_REP, "Stream Pipe", pipefd[0], pipefd[1]); /* do the test */
if (status < 0)
FATAL("read_write_close()"); /* check for error */
COUNT_END("stream_pipe");
return 0;
}
/**********************************************************
* Datagram Pipes
**********************************************************/
int dgram_pipe() {
int status, pipefd[2];
#ifndef NO_SOCKETPAIR
status = socketpair(AF_UNIX, SOCK_DGRAM, 0, pipefd); /* create datagram sockets */
#endif
if (status < 0)
FATAL("Cannot create datagram pipes"); /* if error, stop */
status = read_write_close(DGRAM_PIPE_REP, "Datagram Pipe", pipefd[0], pipefd[1]); /* do the test */
if (status < 0)
FATAL("read_write_close()"); /* check for error */
COUNT_END("dgram_pipe");
return 0;
}
/**********************************************************
* Pipes
**********************************************************/
int pipe_cpy() {
int status, pipefd[2];
status = pipe(pipefd); /* create pipe */
if (status < 0)
FATAL("Cannot create pipes"); /* if error, stop */
status = read_write_close(PIPE_CPY_REP, "Pipe", pipefd[0], pipefd[1]); /* do the test */
if (status < 0)
FATAL("read_write_close()"); /* check for error */
COUNT_END("pipe_cpy");
return 0;
}
static int readn(int fd, char *buf, int size) {
int count, total, result;
count = size + 2; /* maximum iteration count */
total = size; /* initial amount to read */
while (total > 0) { /* while not done */
errno = 0; /* clear errors */
result = read(fd, buf, total); /* read some */
if (result < 0) {
if (errno == EINTR)
continue; /* try again if interrupted */
return result; /* return errors here */
}
total -= result; /* else reduce total */
buf += result; /* update pointer */
if (--count <= 0) {
fprintf(stderr, "\nMaximum iterations exceeded in readn(%d, %lx, %d)", fd, (uintptr_t)buf, size);
return (-1);
}
} /* and loop */
return (size - total); /* calculate # bytes read */
}
static int
writen(int fd,
char *buf,
int size)
{
int
count, total, result;
count = size + 2; /* initialize max count */
total = size; /* total */
while (total > 0) { /* while not done */
errno = 0; /* clear error number */
result = write(fd, buf, total); /* write some */
if (result < 0) { /* handle unusual case */
if (errno == EINTR)
continue; /* if interrupted, loop */
return result; /* return errors here */
}
total -= result; /* else reduce total */
buf += result; /* update pointer */
if (--count <= 0) { /* handle too many loops */
fprintf(stderr, "\nMaximum iterations exceeded in writen(%d, %lx, %d)", fd, (uintptr_t)buf, size);
return (-1);
}
} /* and loop */
return (size - total); /* calculate # bytes read */
}
static int
read_write_close(int loops,
char *title,
int rd_fd,
int wr_fd)
{ /* the actual test */
char buffer[MAXBLK]; /* input and output buffer */
int
size, /* transaction size */
result; /* status result */
COUNT_ZERO; /* clear the count */
while (--loops > 0) {
size = sizes[count++ % Members(sizes)]; /* get next size */
result = writen(wr_fd, buffer, size); /* write this much */
if (result != size) {
fprintf(stderr, "Write returned %d -- ", result);
perror("pipe write");
return (-1);
}
result = readn(rd_fd, buffer, size); /* read this much */
if (result != size) {
fprintf(stderr, "Read returned %d -- ", result);
perror("pipe read");
return (-1);
}
COUNT_BUMP;
}
close(rd_fd);
close(wr_fd);
return (0);
}
#ifdef BSDMEMMAP
/* WARNING: this code will go away next release.
* It is to be used by pure BSD systems w/o shared memory
* and semaphores only!
*
* create a shared memory segment, if it doesnot exist
* ARGS shmfile - disk file for shared memory
*
* RETURNS 0 - successfully, fills in 'shmid'
* -1 - abnormal termination
*/
static int
create_shm(char *shmfile)
{
static char buffer[MAXBLK]; /* arbitrary buffer for file initialization */
/*
* first attempt to open file, if failed then create file.
*/
shmid = open(shmfile, O_RDWR | O_CREAT | O_EXCL, 0666);
if (shmid == -1) {
fprintf(stderr, "create_shm(): create and open of %s failed",
shmfile);
return (-1);
}
/*
* write something in the file to make it big enough to hold the shared data.
*/
if (write(shmid, buffer, MAXBLK) == -1) {
fprintf(stderr, "create_shm(): write file desc %d failure.\n",
shmid);
close(shmid);
unlink(shmfile);
return (-1);
}
unlink(shmfile); /* can still write to it */
return (0);
}
#else
/*
* loops until both shared memory and semaphore are available.
* returns semaphore id in "sem" and shared memory id in "shmid"
*/
static void
create_shared_memory(int *sem,
int *shmid)
{
int say_mem = 0, say_sem = 0; /* only tell user once about pause */
for (;;) { /* loop until succeed */
*shmid = shmget(SHMKEY, MAXBLK, IPC_CREAT | 0666); /* shared memory creation */
if (*shmid < 0) { /* try again later */
if (say_mem % 5 == 0) /* don't bother user too much */
fprintf(stderr,
"create_shared_memory(): can't create shared memory, pausing...\n");
sleep(1);
say_mem++;
} else {
*sem = semget(SEMKEY, 1, 0666 | IPC_CREAT); /* create semaphore, init to 0 */
if (*sem < 0) { /* try again later */
shmctl(*shmid, IPC_RMID, 0); /* free the shared memory, let someone else use */
if (say_sem % 5 == 0) /* don't bother user too much */
fprintf(stderr,
"create_shared_memory(): can't create semaphore, pausing...\n");
sleep(1);
say_sem++;
} else
return; /* we have everything we need */
}
} /* end outer shared memory loop */
}
#endif
void
clear_ipc()
{
#ifdef BSDMEMMAP
if (bufptr != NULL) { /* only clear if set */
if (munmap((caddr_t) bufptr, MAXBLK) == -1)
fprintf(stderr,
"clear_ipc(): munmap failure bufptr=%x errno=%d\n",
bufptr, errno);
bufptr = (char *)NULL;
}
if (shmid != -1) { /* only clear if set */
if (close(shmid) == -1) /* Close the memory mapped file */
fprintf(stderr, "clear_ipc(): file close failure");
shmid = -1;
}
#else
if (bufptr != NULL) { /* only clear if set */
if (shmdt(bufptr) < 0) /* unattach the shared memory */
fprintf(stderr,
"clear_ipc(): can't detach shared memory\n");
bufptr = NULL;
}
if (shmid != -1) { /* only clear if set */
if (shmctl(shmid, IPC_RMID, 0) < 0) /* free the shared memory */
fprintf(stderr,
"clear_ipc(): can't remove shared memory\n");
shmid = -1;
}
if (sem != -1) { /* only clear if set */
if (semctl(sem, 0, IPC_RMID, 0) < 0) /* free the semaphore */
fprintf(stderr,
"clear_ipc(): can't remove semaphore\n");
sem = -1;
}
#endif
}
int shared_memory() {
static char buffer[MAXBLK], /* input and output buffer */
shmfile[STRLEN];
int status, size, /* transaction size */
result; /* status result */
#ifndef BSDMEMMAP
struct sembuf sem_wait, sem_signal;
#endif
int n;
COUNT_ZERO; /* clear the count */
/*
* Get directory for shared memory file
*/
/*
* because we must pass an directory to the BSD version, the loop arg is hardwired :(
*/
sprintf(shmfile, "DISKS/%s", SHMFILE); /* append values */
#ifdef BSDMEMMAP
/*
* WARNING: this code will go away next release.
* * It is to be used by pure BSD systems w/o shared memory
* * and semaphores only!
*/
aim_mktemp(shmfile); /* generate new file name */
if (create_shm(shmfile) == -1) { /* create shared memory segment */
return -1;
}
/*
* Attach to the segment
*/
bufptr = (char *)mmap((caddr_t) 0, MAXBLK, PROT_READ | PROT_WRITE,
MAP_SHARED, shmid, (off_t) 0);
if (bufptr == (char *)-1)
FATAL_SHARED_MEM("Unable to attach to shared memory."); /* error handling */
for (n = 0; n < SHARED_MEMORY_REP; n++) {
size = sizes[count++ % Members(sizes)]; /* get the next size */
/*
* Writer Side
*/
result = flock(shmid, LOCK_EX);
if (result < 0)
FATAL_SHARED_MEM("lock for write");
memcpy(bufptr, buffer, size); /* copy data over there */
result = flock(shmid, LOCK_UN);
if (result < 0)
FATAL_SHARED_MEM("unlock for write");
/*
* Reader Side
*/
result = flock(shmid, LOCK_EX);
if (result < 0)
FATAL_SHARED_MEM("lock for read");
memcpy(buffer, bufptr, size); /* copy data over there */
result = flock(shmid, LOCK_UN);
if (result < 0)
FATAL_SHARED_MEM("unlock for read");
COUNT_BUMP;
}
#else
/*
* Create shared memory segment and semaphore
*/
create_shared_memory(&sem, &shmid);
errno = 0;
/*
* Attach to the segment
*/
bufptr = (char *)shmat(shmid, 0, 0); /* attach anywhere system wants */
if (bufptr == (char *)-1)
FATAL_SHARED_MEM("Unable to attach to shared memory."); /* error handling */
semarg.val = 1; /* initialize semaphore to 1 */
status = semctl(sem, 0, SETVAL, semarg); /* do it */
if (status < 0)
FATAL_SHARED_MEM("semctl()"); /* error here */
sem_wait.sem_num = sem_signal.sem_num = 0; /* use first semaphore only */
sem_wait.sem_op = -1; /* reduce by 1 */
sem_signal.sem_op = 1; /* bump by 1 */
/*
* commented out because only a small # of processes at a time can set this
* * we were hitting the limit after 35 users, got "ENOSPC" error
*/
/*
* sem_wait.sem_flg = sem_signal.sem_flg = SEM_UNDO;
*//*
* handle at the end
*/
sem_wait.sem_flg = sem_signal.sem_flg = 0; /* inititalize flag 1/22/96 */
for (n = 0; n < 100; n++) {
size = sizes[count++ % Members(sizes)]; /* get the next size */
/*
* Writer Side
*/
result = semop(sem, &sem_wait, 1); /* lock on first one */
if (result < 0)
FATAL_SHARED_MEM("sem_wait");
memcpy(bufptr, buffer, size); /* copy data over there */
result = semop(sem, &sem_signal, 1); /* free it */
if (result < 0)
FATAL_SHARED_MEM("sem_signal");
/*
* Reader Side
*/
result = semop(sem, &sem_wait, 1); /* lock on first one */
if (result < 0)
FATAL_SHARED_MEM("sem_wait");
memcpy(buffer, bufptr, size); /* copy data over there */
result = semop(sem, &sem_signal, 1); /* free it */
if (result < 0)
FATAL_SHARED_MEM("sem_signal");
COUNT_BUMP;
}
#endif
clear_ipc(); /* free up shared memory and semaphore */
return 0; /* success */
}