Add lostfd test, rename twowrites to multiwrite

Signed-off-by: Keith Packard <keithp@keithp.com>
This commit is contained in:
Keith Packard 2012-10-05 12:25:30 -07:00
parent f05b97d385
commit da7192d76c
4 changed files with 100 additions and 7 deletions

View File

@ -1,12 +1,15 @@
CFLAGS=-O0 -g
all: fdpassing twowrites xreq
all: fdpassing multiwrite lostfd xreq
fdpassing: fdpassing.o fdpass.o
$(CC) $(CFLAGS) -o $@ fdpassing.o fdpass.o
twowrites: twowrites.o fdpass.o
$(CC) $(CFLAGS) -o $@ twowrites.o fdpass.o
multiwrite: multiwrite.o fdpass.o
$(CC) $(CFLAGS) -o $@ multiwrite.o fdpass.o
lostfd: lostfd.o fdpass.o
$(CC) $(CFLAGS) -o $@ lostfd.o fdpass.o
xreq: xreq.o fdpass.o
$(CC) $(CFLAGS) -o $@ xreq.o fdpass.o

View File

@ -24,10 +24,17 @@ child(int sock)
char buf[16];
ssize_t size;
size = sock_fd_read(sock, buf, sizeof(buf), &fd);
printf ("read %d\n", size);
if (fd != -1)
write(fd, "hello, world\n", 13);
sleep(1);
for (;;) {
size = sock_fd_read(sock, buf, sizeof(buf), &fd);
if (size <= 0)
break;
printf ("read %d\n", size);
if (fd != -1) {
write(fd, "hello, world\n", 13);
close(fd);
}
}
}
void

80
lostfd.c Normal file
View File

@ -0,0 +1,80 @@
/*
* Copyright © 2012 Keith Packard <keithp@keithp.com>
*
* 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; version 2 of the License.
*
* 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 "fdpass.h"
void
child(int sock)
{
int fd;
char buf[16];
ssize_t size;
sleep(1);
size = sock_fd_read(sock, buf, sizeof(buf), NULL);
if (size <= 0)
return;
printf ("read %d\n", size);
size = sock_fd_read(sock, buf, sizeof(buf), &fd);
if (size <= 0)
return;
printf ("read %d\n", size);
if (fd != -1) {
write(fd, "hello, world\n", 13);
close(fd);
}
}
void
parent(int sock)
{
ssize_t size;
int i;
int fd;
fd = 1;
size = sock_fd_write(sock, "1", 1, 1);
printf ("wrote %d without fd\n", size);
size = sock_fd_write(sock, "1", 1, 2);
printf ("wrote %d with fd\n", size);
}
int
main(int argc, char **argv)
{
int sv[2];
int pid;
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) < 0) {
perror("socketpair");
exit(1);
}
switch ((pid = fork())) {
case 0:
close(sv[0]);
child(sv[1]);
break;
case -1:
perror("fork");
exit(1);
default:
close(sv[1]);
parent(sv[0]);
break;
}
return 0;
}

View File

@ -24,6 +24,7 @@ child(int sock)
char buf[16];
ssize_t size;
sleep(1);
for (;;) {
size = sock_fd_read(sock, buf, sizeof(buf), &fd);
if (size <= 0)
@ -48,6 +49,8 @@ parent(int sock)
printf ("wrote %d without fd\n", size);
size = sock_fd_write(sock, "1", 1, 1);
printf ("wrote %d with fd\n", size);
size = sock_fd_write(sock, "1", 1, -1);
printf ("wrote %d without fd\n", size);
}
int