From f86cf8988bce70cb1aecf614cd338de816112889 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Tue, 3 Feb 2015 08:00:33 +0100 Subject: [PATCH] Move stuff around. Try to reduce size rofi.c. --- include/helper.h | 7 ++++++ source/helper.c | 52 ++++++++++++++++++++++++++++++++++++++--- source/rofi.c | 61 ++++++------------------------------------------ 3 files changed, 63 insertions(+), 57 deletions(-) diff --git a/include/helper.h b/include/helper.h index 98f18627..a4429e8b 100644 --- a/include/helper.h +++ b/include/helper.h @@ -138,4 +138,11 @@ int token_match ( char **tokens, const char *input, int case_sensitive, * @returns a valid file descriptor on success, or -1 on failure. */ int execute_generator ( char * cmd ) __attribute__( ( nonnull ) ); + +/** + * @param pidfile The pidfile to create. + * + * Calls exit (1) when failed. + */ +void create_pid_file ( const char *pidfile ); #endif // __HELPER_H__ diff --git a/source/helper.c b/source/helper.c index e04a6ea2..02559a3c 100644 --- a/source/helper.c +++ b/source/helper.c @@ -1,10 +1,15 @@ +#include #include #include #include #include -#include -#include -#include +#include +#include +#include +#include +#include +#include "helper.h" +#include "rofi.h" /** * `fgets` implementation with custom separator. @@ -190,6 +195,9 @@ int find_arg_str_alloc ( const int argc, char * const argv[], const char * const int i = find_arg ( argc, argv, key ); if ( val != NULL && i > 0 && i < argc - 1 ) { + if ( *val != NULL ) { + g_free ( *val ); + } *val = g_strdup ( argv[i + 1] ); return TRUE; } @@ -331,3 +339,41 @@ int execute_generator ( char * cmd ) g_strfreev ( args ); return fd; } + + +void create_pid_file ( const char *pidfile ) +{ + if ( pidfile == NULL ) { + return; + } + + int fd = open ( pidfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR ); + if ( fd < 0 ) { + fprintf ( stderr, "Failed to create pid file." ); + exit ( EXIT_FAILURE ); + } + // Set it to close the File Descriptor on exit. + int flags = fcntl ( fd, F_GETFD, NULL ); + flags = flags | FD_CLOEXEC; + if ( fcntl ( fd, F_SETFD, flags, NULL ) < 0 ) { + fprintf ( stderr, "Failed to set CLOEXEC on pidfile." ); + close ( fd ); + exit ( EXIT_FAILURE ); + } + // Try to get exclusive write lock on FD + int retv = flock ( fd, LOCK_EX | LOCK_NB ); + if ( retv != 0 ) { + fprintf ( stderr, "Failed to set lock on pidfile: Rofi already running?\n" ); + fprintf ( stderr, "%d %s\n", retv, strerror ( errno ) ); + exit ( EXIT_FAILURE ); + } + if ( ftruncate ( fd, (off_t) 0 ) == 0 ) { + // Write pid, not needed, but for completeness sake. + char buffer[64]; + int length = snprintf ( buffer, 64, "%i", getpid () ); + ssize_t l = 0; + while ( l < length ) { + l += write ( fd, &buffer[l], length - l ); + } + } +} diff --git a/source/rofi.c b/source/rofi.c index 79931d7e..5f8f25b0 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -46,10 +45,8 @@ #include #include -#include #include - #include "helper.h" #include "rofi.h" #include "rofi-i3.h" @@ -65,10 +62,10 @@ #define LINE_MARGIN 3 // This setting is no longer user configurable, but partial to this file: -int config_i3_mode = 0; +int config_i3_mode = 0; -char *pidfile = NULL; -static void create_pid_file ( const char *pidfile ); +// Pidfile. +char *pidfile = NULL; const char *cache_dir = NULL; unsigned int NumlockMask = 0; @@ -2107,9 +2104,7 @@ SwitcherMode run_switcher_window ( char **input, G_GNUC_UNUSED void *data ) static int run_dmenu () { // Create pid file - if ( pidfile != NULL ) { - create_pid_file ( pidfile ); - } + create_pid_file ( pidfile ); // Request truecolor visual. create_visual_and_colormap (); @@ -2150,9 +2145,7 @@ static void run_switcher ( int do_fork, SwitcherMode mode ) XSync ( display, True ); } // Create pid file - if ( pidfile != NULL ) { - create_pid_file ( pidfile ); - } + create_pid_file ( pidfile ); create_visual_and_colormap (); @@ -2607,45 +2600,6 @@ static void hup_action_handler ( int num ) } } } -/** - * @param pidfile The pidfile to create. - */ -static void create_pid_file ( const char *pidfile ) -{ - if ( pidfile == NULL ) { - return; - } - - int fd = open ( pidfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR ); - if ( fd < 0 ) { - fprintf ( stderr, "Failed to create pid file." ); - exit ( 1 ); - } - // Set it to close the File Descriptor on exit. - int flags = fcntl ( fd, F_GETFD, NULL ); - flags = flags | FD_CLOEXEC; - if ( fcntl ( fd, F_SETFD, flags, NULL ) < 0 ) { - fprintf ( stderr, "Failed to set CLOEXEC on pidfile." ); - close ( fd ); - exit ( 1 ); - } - // Try to get exclusive write lock on FD - int retv = flock ( fd, LOCK_EX | LOCK_NB ); - if ( retv != 0 ) { - fprintf ( stderr, "Failed to set lock on pidfile: Rofi already running?\n" ); - fprintf ( stderr, "%d %s\n", retv, strerror ( errno ) ); - exit ( 1 ); - } - if ( ftruncate ( fd, (off_t) 0 ) == 0 ) { - // Write pid, not needed, but for completeness sake. - char buffer[64]; - int length = snprintf ( buffer, 64, "%i", getpid () ); - ssize_t l = 0; - while ( l < length ) { - l += write ( fd, &buffer[l], length - l ); - } - } -} int main ( int argc, char *argv[] ) { @@ -2720,9 +2674,8 @@ int main ( int argc, char *argv[] ) char *msg = NULL; if ( find_arg_str ( argc, argv, "-e", &( msg ) ) ) { // Create pid file - if ( pidfile != NULL ) { - create_pid_file ( pidfile ); - } + create_pid_file ( pidfile ); + // Request truecolor visual. create_visual_and_colormap (); textbox_setup ( &vinfo, map,