GRASS Programmer's Manual  6.4.2(2012)
connect_sock.c
Go to the documentation of this file.
00001 
00002 #include <grass/config.h>
00003 
00004 #ifdef HAVE_SOCKET
00005 
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 #include <unistd.h>
00010 #include <errno.h>
00011 #include <sys/time.h>
00012 #include <sys/types.h>
00013 
00014 #include <grass/gis.h>
00015 #include "driverlib.h"
00016 
00017 int prepare_connection_sock(const char *me)
00018 {
00019     const char *connpath;
00020     int fd;
00021 
00022     connpath = G_sock_get_fname(me);
00023     if (!connpath)
00024         G_fatal_error("Couldn't get socket path");
00025 
00026     /* Now we must check and see whether or not someone */
00027     /* (possibly another invocation of ourself) is using our socket.    */
00028 
00029     if (G_sock_exists(connpath)) {
00030         if ((fd = G_sock_connect(connpath)) >= 0) {
00031             close(fd);
00032             G_warning("Graphics driver [%s] is already running", me);
00033             G_fatal_error("Unable to start monitor <%s>", me);
00034         }
00035 
00036         if (unlink(connpath) < 0) {
00037             G_warning("Failed to remove stale socket file: %s", connpath);
00038             G_fatal_error("Unable to start monitor <%s>", me);
00039         }
00040     }
00041 
00042     /* We are free to run now.  No one is using our socket.                   */
00043     if ((fd = G_sock_bind(connpath)) < 0) {
00044         G_fatal_error("Can't bind to socket: error \"%s\"\n",
00045                       strerror(errno));
00046     }
00047 
00048     /* Now set up listen */
00049     if (G_sock_listen(fd, 1) != 0) {
00050         G_fatal_error("G_sock_listen: error \"%s\"\n", strerror(errno));
00051     }
00052 
00053     return fd;
00054 }
00055 
00056 int get_connection_sock(int listenfd, int *rfd, int *wfd, int other_fd)
00057 {
00058     int fd;
00059 
00060 #ifndef __MINGW32__
00061     if (other_fd >= 0) {
00062         fd_set waitset;
00063 
00064         FD_ZERO(&waitset);
00065         FD_SET(listenfd, &waitset);
00066         FD_SET(other_fd, &waitset);
00067         if (select(FD_SETSIZE, &waitset, NULL, NULL, NULL) < 0) {
00068             perror("get_connection_sock: select");
00069             exit(EXIT_FAILURE);
00070         }
00071 
00072         if (!FD_ISSET(listenfd, &waitset))
00073             return -1;
00074     }
00075 #endif
00076 
00077     /* G_sock_accept will block until a connection is requested */
00078     fd = G_sock_accept(listenfd);
00079     if (fd >= 0) {
00080         *rfd = fd;
00081         *wfd = dup(fd);
00082         return 0;
00083     }
00084 
00085     if (errno == EINTR)
00086         return -1;
00087 
00088     G_warning("G_sock_accept: error \"%s\"", strerror(errno));
00089     COM_Graph_close();
00090     exit(EXIT_FAILURE);
00091 }
00092 
00093 #endif /* HAVE_SOCKET */
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines