// fifo_init_internal(path) // If path is NULL, reopen existing fifo, else open anew. static int fifo_init_internal(const char *fifo_path) { if (fifo_path) { fifo_deinit(); fifo_name = expand_filename(fifo_path); if (!check_fifo(fifo_name)) { scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO. " "%s already exists and is not a pipe", fifo_name); g_free(fifo_name); fifo_name = NULL; return -1; } } else if (fifo_name) g_source_remove_by_user_data(fifo_channel); else return -1; if (!attach_fifo(fifo_name)) { scr_LogPrint(LPRINT_LOGNORM, "Error: Cannot open fifo"); return -1; } setenv(FIFO_ENV_NAME, fifo_name, 1); scr_LogPrint(LPRINT_LOGNORM, "FIFO initialized (%s)", fifo_path); return 1; }
static guint fifo_callback(GIOChannel *channel, GIOCondition condition, gpointer data) { if (condition & (G_IO_IN|G_IO_PRI)) { GIOStatus chstat; gchar *buf; gsize endpos; chstat = g_io_channel_read_line(channel, &buf, NULL, &endpos, NULL); if (chstat == G_IO_STATUS_ERROR || chstat == G_IO_STATUS_EOF) { if (!attach_fifo(fifo_name)) scr_LogPrint(LPRINT_LOGNORM, "Reopening fifo failed! Fifo will not work from now!"); return FALSE; } if (buf) { guint logflag; guint fifo_ignore = settings_opt_get_int("fifo_ignore"); if (endpos) buf[endpos] = '\0'; if (settings_opt_get_int("fifo_hide_commands")) logflag = LPRINT_LOG; else logflag = LPRINT_LOGNORM; scr_LogPrint(logflag, "%s FIFO command: %s", (fifo_ignore ? "Ignoring" : "Executing"), buf); if (!fifo_ignore) { process_command(buf, TRUE); } g_free(buf); } } else if (condition & (G_IO_ERR|G_IO_NVAL|G_IO_HUP)) { if (!attach_fifo(fifo_name)) scr_LogPrint(LPRINT_LOGNORM, "Reopening fifo failed! Fifo will not work from now!"); return FALSE; } return TRUE; }
/*@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; }