/* Test of socketpair call Copyright (C) 2011 Free Software Foundation, Inc. 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 3 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, see . */ #include #include #include #include #include #include #include #include #include #include #include "macros.h" int testsocketpair (const int familly, const int type, const int protocol) { int pair[2]; int pid; int err; char buffer[512]; const char datasend[] = "ItisAgoodDayforSocketpair"; const char datareply[] = "Forsure"; if (socketpair (familly, type, protocol, pair) < 0) { perror ("socketpair"); return 1; } pid = fork (); if (pid == -1) { perror ("fork"); return 2; } /* parent return code >2 */ if (0 != pid) { int status; int ret; err = close (pair[0]); if (err != 0) { perror ("close father [0]"); return 3; } if (read (pair[1], buffer, sizeof (datasend)) < 0) { perror ("read send"); return 4; } if (0 != strncmp (buffer, datasend, sizeof (datasend))) { perror ("Receive data send does not match"); return 5; } if (write (pair[1], datareply, sizeof (datareply)) < 0) { perror ("writing reply"); return 6; } err = close (pair[1]); if (err != 0) { perror ("close father [1]"); return 7; } /* wait child now */ ret = waitpid (pid, &status, 0); if (ret == -1) { perror ("waitpid"); return 8; } ASSERT (ret == pid); if (!WIFEXITED (status)) { fprintf (stderr, "Child does not normally exit\n"); return 9; } ret = WEXITSTATUS (status); if (ret != 0) return ret + 9; return 0; } /* child */ else { err = close (pair[1]); if (err != 0) { perror ("close child [1]"); exit (1); } if (write (pair[0], datasend, sizeof (datasend)) < 0) { perror ("write on child"); exit (2); } if (read (pair[0], buffer, sizeof (datareply)) < 0) { perror ("read reply child"); exit (3); } if (0 != strncmp (buffer, datareply, sizeof (datareply))) { perror ("Receive data reply does not match"); exit (4); } err = close (pair[0]); if (err != 0) { perror ("close child [1]"); exit (5); } exit (0); } } int testsocketpairfamilly2 (const int familly, const char *famstring) { int retstream; int retdgram; printf ("test protocol familly %s\n", famstring); retstream = testsocketpair (familly, SOCK_STREAM, 0); retdgram = testsocketpair (familly, SOCK_DGRAM, 0); return (! !retstream | (! !retdgram) << 1); } #define testsocketpairfamilly(f) testsocketpairfamilly2(f,#f) int main () { int ret; ret = 0; #ifdef AF_LOCAL ret |= testsocketpairfamilly (AF_LOCAL); #endif #ifdef AF_UNIX ret |= (testsocketpairfamilly (AF_UNIX) << 2); #endif #ifdef AF_INET ret |= (testsocketpairfamilly (AF_INET) << 4); #endif #ifdef AF_INET6 ret |= (testsocketpairfamilly (AF_INET6) << 6); #endif return ret; }