static void create_socket() { GIOChannel *chan = NULL; int sock, len; struct sockaddr_un local; build_stream_name(SOCKET); sock = socket (AF_UNIX, SOCK_STREAM, 0); local.sun_family = AF_UNIX; strcpy (local.sun_path, socket_path); unlink (local.sun_path); len = strlen (local.sun_path) + sizeof (local.sun_family); bind (sock, (struct sockaddr *) &local, len); if (errno == -1) { printf ("Socket: Could not open in %s: %s\n", socket_path, strerror(errno)); } else { printf ("Socket: Opened in %s\n", socket_path); listen (sock, 5); if( (chan = g_io_channel_unix_new(sock)) ) g_io_add_watch(chan, G_IO_IN|G_IO_HUP, (GIOFunc) control_socket, chan); } }
static void create_fifo() { GIOChannel *chan = NULL; GError *error = NULL; build_stream_name(FIFO); if (file_exists(fifo_path)) { g_error ("Fifo: Error when creating %s: File exists\n", fifo_path); return; } if (mkfifo (fifo_path, 0666) == -1) { g_error ("Fifo: Error when creating %s: %s\n", fifo_path, strerror(errno)); } else { // we don't really need to write to the file, but if we open the file as 'r' we will block here, waiting for a writer to open the file. chan = g_io_channel_new_file((gchar *) fifo_path, "r+", &error); if (chan) { if (!g_io_add_watch(chan, G_IO_IN|G_IO_HUP, (GIOFunc) control_fifo, NULL)) { g_error ("Fifo: could not add watch on %s\n", fifo_path); } else { printf ("Fifo: created successfully as %s\n", fifo_path); } } else { g_error ("Fifo: Error while opening: %s\n", error->message); } } return; }
/*@null@*/ gchar* init_socket(gchar *dir) { /* return dir or, on error, free dir and return NULL */ if (uzbl.comm.socket_path) { /* remove an existing socket should one exist */ if (unlink(uzbl.comm.socket_path) == -1) g_warning ("init_socket: couldn't unlink socket at %s\n", uzbl.comm.socket_path); g_free(uzbl.comm.socket_path); uzbl.comm.socket_path = NULL; } if (*dir == ' ') { g_free(dir); return NULL; } struct sockaddr_un local; gchar *path = build_stream_name(SOCKET, dir); local.sun_family = AF_UNIX; strcpy (local.sun_path, path); if(!file_exists(path) && attach_socket(path, &local)) { /* it's free for the taking. */ return dir; } else { /* see if anybody's listening on the socket path we want. */ int sock = socket (AF_UNIX, SOCK_STREAM, 0); if(connect(sock, (struct sockaddr *) &local, sizeof(local)) < 0) { /* some error occurred, presumably nobody's listening. * we can attach ourselves to it. */ unlink(path); if(attach_socket(path, &local)) return dir; else g_warning("init_socket: can't attach to existing socket %s: %s\n", path, strerror(errno)); } else { /* somebody's there, we can't use that socket path. */ close(sock); /* whatever, this instance can live without a socket. */ g_warning ("init_socket: can't create %s: socket exists and is occupied\n", path); } } /* if we got this far, there was an error; cleanup */ g_free(path); g_free(dir); return NULL; }
/*@null@*/ gchar* init_fifo(gchar *dir) { /* return dir or, on error, free dir and return NULL */ if (uzbl.comm.fifo_path) { /* get rid of the old fifo if one exists */ if (unlink(uzbl.comm.fifo_path) == -1) g_warning ("Fifo: Can't unlink old fifo at %s\n", uzbl.comm.fifo_path); g_free(uzbl.comm.fifo_path); uzbl.comm.fifo_path = NULL; } gchar *path = build_stream_name(FIFO, dir); if (!file_exists(path)) { if (mkfifo (path, 0666) == 0 && attach_fifo(path)) { return dir; } else g_warning ("init_fifo: can't create %s: %s\n", path, strerror(errno)); } else { /* the fifo exists. but is anybody home? */ int fd = open(path, O_WRONLY|O_NONBLOCK); if(fd < 0) { /* some error occurred, presumably nobody's on the read end. * we can attach ourselves to it. */ if(attach_fifo(path)) return dir; else g_warning("init_fifo: can't attach to %s: %s\n", path, strerror(errno)); } else { /* somebody's there, we can't use that fifo. */ close(fd); /* whatever, this instance can live without a fifo. */ g_warning ("init_fifo: can't create %s: file exists and is occupied\n", path); } } /* if we got this far, there was an error; cleanup */ g_free(dir); g_free(path); return NULL; }