Index: src/preferences.h =================================================================== --- src/preferences.h (revision 353) +++ src/preferences.h (working copy) @@ -24,6 +24,7 @@ #define ACTIONS_FILE "parcellite/actions" #define FIFO_FILE_C "parcellite/fifo_c" #define FIFO_FILE_P "parcellite/fifo_p" +#define PID_FILE "parcellite/daemon.pid" #define PREFERENCES_FILE "parcellite/parcelliterc" /*struct pref_item* get_pref(char *name); */ Index: src/main.c =================================================================== --- src/main.c (revision 353) +++ src/main.c (working copy) @@ -24,6 +24,9 @@ #include #endif +#include +#include + #include "parcellite.h" @@ -388,7 +391,7 @@ { if(NULL != appindicator_process && !have_appindicator ){ g_printf("Looking for '%s'\n",appindicator_process); - if(proc_find(appindicator_process,PROC_MODE_STRSTR,NULL) >0){ + if(proc_find(appindicator_process,NULL) >0){ have_appindicator=1; if(NULL == indicator && show_icon) create_app_indicator(); @@ -1899,7 +1902,21 @@ { struct cmdline_opts *opts; int mode; - + gboolean is_first_instance = FALSE; + + /* PID file creation */ + check_dirs(); + if (!flock(open(g_build_filename(g_get_user_data_dir(), PID_FILE, NULL), O_CREAT | O_RDWR, 0640), LOCK_EX | LOCK_NB)) + is_first_instance = TRUE; + else + { + if (errno != EWOULDBLOCK) + { + perror("unable to check running instances"); + return 1; + } + } + bindtextdomain(GETTEXT_PACKAGE, PARCELLITELOCALEDIR); bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); textdomain(GETTEXT_PACKAGE); @@ -1911,7 +1928,7 @@ opts=parse_options(argc, argv); if(NULL == opts) return 1; - if(proc_find(PARCELLITE_PROG_NAME,PROC_MODE_EXACT,NULL)<2) /**1 for me, and 1 for a running instance */ + if(is_first_instance) mode=PROG_MODE_DAEMON; /**first instance */ else mode=PROG_MODE_CLIENT; /**already running, just access fifos & exit. */ @@ -1943,6 +1960,7 @@ } else if(opts->clipboard){ fifo=init_fifo(FIFO_MODE_CLI|mode); + if(fifo->dbg) g_printf("Hit CLI opt!\n"); if(PROG_MODE_CLIENT & mode){ if(NULL != opts->leftovers){ Index: src/utils.h =================================================================== --- src/utils.h (revision 353) +++ src/utils.h (working copy) @@ -62,7 +62,7 @@ gint which; /**which clipboard the buffer should be written to */ gint dbg; /**set to debug fifo system */ }; -int proc_find(const char* name, int mode, pid_t *pid); +int proc_find(const char* name, pid_t *pid); int create_fifo(void); int open_fifos(struct p_fifo *fifo); int read_fifo(struct p_fifo *f, int which); Index: src/utils.c =================================================================== --- src/utils.c (revision 353) +++ src/utils.c (working copy) @@ -184,7 +184,7 @@ \n\b Arguments: \n\b Returns: number of instances of name found ****************************************************************************/ -int proc_find(const char* name, int mode, pid_t *pid) +int proc_find(const char* name, pid_t *pid) { DIR* dir; FILE* fp; @@ -210,22 +210,12 @@ if ((fp= fopen(buf, "r"))) { if (fgets(buf, sizeof(buf), fp) != NULL) { - if(PROC_MODE_EXACT == mode){ - gchar *b=g_path_get_basename(buf); - if (!g_strcmp0(b, name)) { - ++instances; - if(NULL !=pid) - *pid=lpid; - } - }else if( PROC_MODE_STRSTR == mode){ - if(NULL !=g_strrstr(buf,name)){ - ++instances; - if(NULL !=pid) - *pid=lpid; - /*g_printf("Looking for '%s', found '%s'\n",name,buf); */ - } - } - + if(NULL !=g_strrstr(buf,name)){ + ++instances; + if(NULL !=pid) + *pid=lpid; + /*g_printf("Looking for '%s', found '%s'\n",name,buf); */ + } } fclose(fp); }