sendmsg SCM_RIGHTS problem

From: Andrey G. Kaplanov (A.Kaplanov@compassplus.ru)
Date: Mon Sep 11 2000 - 07:31:34 EST


Respected colleagues!

I have are problem of send SCM_RIGHTS message
through AF_UNIX socket.
Below - examples of server and client sources.
Sendmsg gives an error : Invalid argument.
That I do;make wrong?

//////////////////////////// Server /////////////////////////////////

# include <unistd.h>
# include <stdio.h>
# include <string.h>
# include <fcntl.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/stropts.h>
# include <sys/socket.h>
# include <sys/errno.h>
# include <sys/timex.h>
# include <linux/un.h>

void main()
{
    sockaddr_un addr;
    int sock = socket(AF_UNIX, SOCK_STREAM, 0);
    addr.sun_family = AF_UNIX;
    memset(addr.sun_path, 0, UNIX_PATH_MAX);
    strncpy(addr.sun_path, "sock", UNIX_PATH_MAX-1);
    unlink("sock");
    bind(sock, (sockaddr *)&addr, sizeof(sockaddr_un));
    listen(sock, 5);
    if (accept(sock, (struct sockaddr*)0, (size_t *)0) == -1)
    {
        fprintf(stderr, "accept error %s\n", strerror(errno));
        exit(1);
    }
    printf("accepted\n");
    getchar();
}

////////////////////////// Client //////////////////////////////////

# include <unistd.h>
# include <stdio.h>
# include <string.h>
# include <fcntl.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/stropts.h>
# include <sys/socket.h>
# include <sys/errno.h>
# include <sys/timex.h>
# include <linux/un.h>

void main()
{
    sockaddr_un addr;
    int dsc = socket(PF_UNIX, SOCK_STREAM, 0);
    addr.sun_family = AF_UNIX;
    strcpy(addr.sun_path, "sock");
    if (::connect(dsc, (sockaddr *)&addr, sizeof(sockaddr_un)) != 0)
    {
        fprintf(stderr, "connect error %s\n", strerror(errno));
        exit(1);
    }
    msghdr msg;
    char cbuf[CMSG_SPACE(CMSG_LEN(sizeof(int)))];
    msg.msg_name = 0;
    msg.msg_namelen = 0;
    msg.msg_iov = 0;
    msg.msg_iovlen = 0;
    msg.msg_control = cbuf;
    msg.msg_controllen = CMSG_SPACE(CMSG_LEN(sizeof(int)));
    msg.msg_flags = 0;
    cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_len = CMSG_LEN(sizeof(int));
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    *((int *)CMSG_DATA(cmsg)) = dsc;
    if (sendmsg(dsc, &msg, 0) == -1)
    {
        fprintf(stderr, "sendmsg error %s\n", strerror(errno));
        exit(1);
    }
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Fri Sep 15 2000 - 21:00:14 EST