What am I doing wrong here (UNIX-domain sockets don't work)?

What am I doing wrong here (UNIX-domain sockets don't work)?

Post by Benjamin Z. Goldste » Fri, 15 Jul 1994 16:13:00



Hello,
     I am trying to put together a suite of benchmarks.  Some of the
benchmarks I am using are based on the BSD benchmarks (circa 1985
Anyway, I am updating and reviewing these benchmarks, and I can't get
this one to work on DG/UX 5.4.  I probably made a bonehead mistake
(like the time I was mismatching termios element structures and flags).
It does work on the IBM RS-6000/AIX 3.2.5, i386/FreeBSD 1.1, and
SGI Indigo/IRIX 4.0.5 (though only at 300KB/sec).  Any advice appreciated!

/*
 * IPC benchmark,
 * read and reply using UNIX domain datagram sockets.
 *
 * Process forks and exchanges messages using a
 * UNIX domain datagram socket in a request-response fashion.
 */

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>

#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <sys/resource.h>

/* These prototypes were necessary for DG/UX 5.4 */
int     socket(int, int, int);
int     setsockopt(int, int, int, const void *, int);
int     getsockopt(int, int, int, void *, int *);
int     bind(int, const struct sockaddr *, int);
int     sendto(int, const void *, int, int, const struct sockaddr *, int);
int     recvfrom(int, void *, int, int, struct sockaddr *, int *);
#ifndef SUN_LEN
#define SUN_LEN(su) \
        (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
#endif

void    catchsig();

struct  sockaddr_un sun;
struct  sockaddr_un myname;
int i;

int
main(int argc, char *argv[]) {
        int             optlen = sizeof(i);
        int             j, pid, sfd, sunlen;
        int             msgs = 0, msglen = 0;
        char            *buf;
        struct sigaction        anaction;

        /* Option parsing */
        if (argc < 3) {
                printf("usage: %s #msgs msglen\n", argv[0]);
                exit(1);
        }
        msgs = atoi(argv[1]);
        msglen = atoi(argv[2]);
        buf = malloc(msglen);
        for(i = 0; i < msglen; i++) buf[i] = i;
        if (buf == 0) {
                printf("Couldn't allocate data buffer\n");
                exit(1);
        }
        j = 2 * i; /* i == msglen after the for() */

        /* Setup socket */
        sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
        if (sfd < 0) {
                perror("socket");
                exit(1);
        }
        myname.sun_family = AF_UNIX;
        sprintf(myname.sun_path, "unixdg%d", getpid());
        sunlen = SUN_LEN(&myname);

        /* Set the buffer sizes in case we have to handle large buffer */
        if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &i, optlen) < 0 ||
            setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, &j, optlen) < 0)
                perror("setsockopt");

        /* Install clean-up code right before bind() */
        anaction.sa_handler = catchsig;
        sigfillset(&anaction.sa_mask);
        anaction.sa_flags = 0;
        sigaction(SIGINT, &anaction, NULL);
        sigaction(SIGTERM, &anaction, NULL);

        /* Attach a name to our socket */
        if (bind(sfd, (const struct sockaddr *) &myname, sunlen) < 0) {
                perror("bind");
                exit(1);
        }

        /* Start the communications loop */
        pid = fork();
        if (pid == 0)
                /* Child process -- waits for parent */
                for (i = 0; i < msgs; i++) {
                        sunlen = sizeof (sun);
                        if (recvfrom(sfd, buf, msglen, 0,
                          (struct sockaddr *) &sun, &sunlen) < 0) {
                                perror("recvfrom (child)");
                                kill(getpid(), SIGTERM);
                        }

                        sunlen = SUN_LEN(&myname);
                        if (sendto(sfd, buf, msglen, 0,
                          (const struct sockaddr *) &myname, sunlen) < 0) {
                                perror("sendto (child)");
                                kill(getpid(), SIGTERM);
                        }
                }
        else {
                /* This is the parent */
                for (i = 0; i < msgs; i++) {
                        sunlen = SUN_LEN(&myname);
                        if (sendto(sfd, buf, msglen, 0,
                          (const struct sockaddr *) &myname, sunlen) < 0) {
                                perror("sendto (parent)");
                                kill(getpid(), SIGTERM);
                        }

                        sunlen = sizeof (sun);
                        while (recvfrom(sfd, buf, msglen, 0,
                          (struct sockaddr *) &sun, &sunlen) < 0) {
                                perror("recvfrom (parent)");
                                kill(getpid(), SIGTERM);
                        }
                }
                /* Cleanup socket in file system */
                unlink(myname.sun_path);
        }
        close(sfd);
        return 0;

Quote:}

void
catchsig() {
        fprintf(stderr,"Packets sent: %d\n", i);
        unlink(myname.sun_path);
        exit(1);
Quote:}

--
Benjamin Z. Golds*
 
 
 

1. X11R5 clients don't work with unix domain sockets

 I've compiled and installed X11R5 on our RS6000 320H (AIX 3.2.1), and it worked
fine. Then I messed up with some InfoExplorer databases and reinstalled some of
the packages from the install tapes. Now X11R5 clients fail to open display
unix:0 while local:0 (and of course localhost:0 and `hostname`:0) work fine.
AIXwindows clients work with both the original and the X11R5 server, so I guess
it's not a problem of unix domain sockets in general.
 Did anyone encounter the same problems? Or better yet, does anybody know what
I may have messed up?

Thnx, Andy

-------------------------------------------------------------------------------



   Germany           !  Doogie Howser, M.D. !  Voice: +49 721 84 10 36

2. what replaced 'dip' ??

3. setenvif doesn't seem to work properly - what am I doing wrong ?

4. kernel semaphore quest + search archives question

5. What am I doing wrong with 'chat'?

6. Mouse detection

7. Can't find /dev/nst0....What am I doing wrong?

8. mount image without device

9. USYNC_PROCESS - what's am I doing wrong

10. Apache's <Location...> Directive - What am I doing wrong?

11. Can't get this to work if Hostname and Domain don't match.

12. Magic SysRq key isn't -- what am I doing wrong?

13. Burning CD's what am I doing wrong?