Main Page | Class List | File List | Class Members | File Members

ud_socket.c

Go to the documentation of this file.
00001 /*
00002  * $Id: ud_socket.c,v 1.4 2003/11/17 21:24:58 sunthockin Exp $
00003  * A few  routines for handling UNIX domain sockets
00004  */
00005 
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <unistd.h>
00009 #include <errno.h>
00010 #include <string.h>
00011 #include <sys/types.h>
00012 #include <sys/stat.h>
00013 #include <sys/socket.h>
00014 #include <sys/un.h>
00015 #include <fcntl.h>
00016 
00017 #include "acpid.h"
00018 #include "ud_socket.h"
00019 
00020 int
00021 ud_create_socket(const char *name)
00022 {
00023         int fd;
00024         int r;
00025         struct sockaddr_un uds_addr;
00026 
00027         /* JIC */
00028         unlink(name);
00029 
00030         fd = socket(AF_UNIX, SOCK_STREAM, 0);
00031         if (fd < 0) {
00032                 return fd;
00033         }
00034 
00035         /* setup address struct */
00036         memset(&uds_addr, 0, sizeof(uds_addr));
00037         uds_addr.sun_family = AF_UNIX;
00038         strcpy(uds_addr.sun_path, name);
00039         
00040         /* bind it to the socket */
00041         r = bind(fd, (struct sockaddr *)&uds_addr, sizeof(uds_addr));
00042         if (r < 0) {
00043                 return r;
00044         }
00045 
00046         /* listen - allow 10 to queue */
00047         r = listen(fd, 10);
00048         if (r < 0) {
00049                 return r;
00050         }
00051 
00052         return fd;
00053 }
00054 
00055 int
00056 ud_accept(int listenfd, struct ucred *cred)
00057 {
00058         while (1) {
00059                 int newsock = 0;
00060                 struct sockaddr_un cliaddr;
00061                 int len = sizeof(struct sockaddr_un);
00062 
00063                 newsock = accept(listenfd, (struct sockaddr *)&cliaddr, &len);
00064                 if (newsock < 0) {
00065                         if (errno == EINTR) {
00066                                 continue; /* signal */
00067                         }
00068                 
00069                         return newsock;
00070                 }
00071 
00072                 if (cred) {
00073                         len = sizeof(struct ucred);
00074                         getsockopt(newsock, SOL_SOCKET, SO_PEERCRED,cred,&len);
00075                 }
00076 
00077                 return newsock;
00078         }
00079 }
00080 
00081 int
00082 ud_connect(const char *name)
00083 {
00084         int fd;
00085         int r;
00086         struct sockaddr_un addr;
00087 
00088         fd = socket(AF_UNIX, SOCK_STREAM, 0);
00089         if (fd < 0) {
00090                 return fd;
00091         }
00092 
00093         memset(&addr, 0, sizeof(addr));
00094         addr.sun_family = AF_UNIX;
00095         sprintf(addr.sun_path, "%s", name);
00096 
00097         r = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
00098         if (r < 0) {
00099                 close(fd);
00100                 return r;
00101         }
00102 
00103         return fd;
00104 }
00105 

© sourcejam.com 2005-2008