Skip to content

Commit

Permalink
[appsync] Use global diag mode state for config dir selection. JB#38342
Browse files Browse the repository at this point in the history
Selection of diagnostic mode is made once during lifetime of usb-moded
process. Passing it around as parameter can only lead to confusion.

Make it so that appsync_read_list() reads configuration files from
directory determined from globally defined diagnostic mode selection
rather than from parameter passed to it.

Use glob pattern for determining list of files to read. As a side effect
this stops usb-moded from reading all files (e.g. editor backup files
accidentally left behind) which could lead into obscure and hard to
figure out problems.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Sep 24, 2020
1 parent 3888e4d commit 89ae6e9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 51 deletions.
6 changes: 6 additions & 0 deletions .depend
Expand Up @@ -32,15 +32,21 @@ src/usb_moded-appsync-dbus.pic.o:\

src/usb_moded-appsync.o:\
src/usb_moded-appsync.c\
config-static.h\
src/usb_moded-appsync.h\
src/usb_moded-dyn-config.h\
src/usb_moded-log.h\
src/usb_moded-systemd.h\
src/usb_moded.h\

src/usb_moded-appsync.pic.o:\
src/usb_moded-appsync.c\
config-static.h\
src/usb_moded-appsync.h\
src/usb_moded-dyn-config.h\
src/usb_moded-log.h\
src/usb_moded-systemd.h\
src/usb_moded.h\

src/usb_moded-common.o:\
src/usb_moded-common.c\
Expand Down
86 changes: 37 additions & 49 deletions src/usb_moded-appsync.c
Expand Up @@ -29,13 +29,15 @@

#include "usb_moded-appsync.h"

#include "usb_moded.h"
#include "usb_moded-log.h"
#include "usb_moded-systemd.h"

#include <sys/time.h>

#include <stdlib.h>
#include <string.h>
#include <glob.h>

/* ========================================================================= *
* Prototypes
Expand All @@ -49,8 +51,8 @@ static void appsync_free_elem (list_elem_t *elem);
static void appsync_free_elem_cb (gpointer elem, gpointer user_data);
void appsync_free_appsync_list (void);
static gint appsync_list_sort_func (gconstpointer a, gconstpointer b);
void appsync_read_list (int diag);
static list_elem_t *appsync_read_file (const gchar *filename, int diag);
void appsync_read_list (void);
static list_elem_t *appsync_read_file (const char *filename);
int appsync_activate_sync (const char *mode);
int appsync_activate_sync_post (const char *mode);
int appsync_mark_active (const gchar *name, int post);
Expand Down Expand Up @@ -120,79 +122,66 @@ static gint appsync_list_sort_func(gconstpointer a, gconstpointer b)
return strcasecmp( (char*)a, (char*)b );
}

void appsync_read_list(int diag)
void appsync_read_list(void)
{
LOG_REGISTER_CONTEXT;

GDir *confdir = 0;

const gchar *dirname;
list_elem_t *list_item;
gchar *pat = 0;
glob_t gb = {};

appsync_free_appsync_list();

if(diag)
{
if( !(confdir = g_dir_open(CONF_DIR_DIAG_PATH, 0, NULL)) )
goto cleanup;
}
else
{
if( !(confdir = g_dir_open(CONF_DIR_PATH, 0, NULL)) )
goto cleanup;
bool diag_mode = usbmoded_get_diag_mode();
const char *conf_dir = diag_mode ? CONF_DIR_DIAG_PATH : CONF_DIR_PATH;
if( !(pat = g_strdup_printf("%s/*.ini", conf_dir)) )
goto cleanup;

if( glob(pat, 0, 0, &gb) != 0 ) {
log_debug("no appsync ini-files found");
goto cleanup;
}

while( (dirname = g_dir_read_name(confdir)) )
{
log_debug("Read file %s\n", dirname);
if( (list_item = appsync_read_file(dirname, diag)) )
for( size_t i = 0; i < gb.gl_pathc; ++i ) {
list_elem_t *list_item = appsync_read_file(gb.gl_pathv[i]);
if( list_item )
appsync_sync_list = g_list_append(appsync_sync_list, list_item);
}

cleanup:
if( confdir ) g_dir_close(confdir);

/* sort list alphabetically so services for a mode
* can be run in a certain order */
appsync_sync_list=g_list_sort(appsync_sync_list, appsync_list_sort_func);
if( appsync_sync_list ) {
/* sort list alphabetically so services for a mode
* can be run in a certain order */
appsync_sync_list = g_list_sort(appsync_sync_list, appsync_list_sort_func);

/* set up session bus connection if app sync in use
* so we do not need to make the time consuming connect
* operation at enumeration time ... */

if( appsync_sync_list )
{
/* set up session bus connection if app sync in use
* so we do not need to make the time consuming connect
* operation at enumeration time ... */
log_debug("Sync list valid\n");
#ifdef APP_SYNC_DBUS
dbusappsync_init_connection();
#endif
}

cleanup:
globfree(&gb);
g_free(pat);
}

static list_elem_t *appsync_read_file(const gchar *filename, int diag)
static list_elem_t *appsync_read_file(const char *filename)
{
LOG_REGISTER_CONTEXT;

gchar *full_filename = NULL;
GKeyFile *settingsfile = NULL;
list_elem_t *list_item = NULL;
list_elem_t *list_item = NULL;
GKeyFile *settingsfile = NULL;

if(diag)
{
if( !(full_filename = g_strconcat(CONF_DIR_DIAG_PATH, "/", filename, NULL)) )
goto cleanup;
}
else
{
if( !(full_filename = g_strconcat(CONF_DIR_PATH, "/", filename, NULL)) )
goto cleanup;
}
log_debug("loading appsync file: %s", filename);

if( !(settingsfile = g_key_file_new()) )
goto cleanup;

if( !g_key_file_load_from_file(settingsfile, full_filename, G_KEY_FILE_NONE, NULL) )
if( !g_key_file_load_from_file(settingsfile, filename, G_KEY_FILE_NONE, NULL) ) {
log_err("failed to load appsync file: %s", filename);
goto cleanup;
}

if( !(list_item = calloc(1, sizeof *list_item)) )
goto cleanup;
Expand All @@ -212,12 +201,11 @@ static list_elem_t *appsync_read_file(const gchar *filename, int diag)

if(settingsfile)
g_key_file_free(settingsfile);
g_free(full_filename);

/* if a minimum set of required elements is not filled in we discard the list_item */
if( list_item && !(list_item->name && list_item->mode) )
{
log_debug("Element invalid, discarding\n");
log_err("discarding invalid appsync file: %s", filename);
appsync_free_elem(list_item);
list_item = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/usb_moded-appsync.h
Expand Up @@ -82,7 +82,7 @@ typedef struct list_elem_t
* ------------------------------------------------------------------------- */

void appsync_free_appsync_list (void);
void appsync_read_list (int diag);
void appsync_read_list (void);
int appsync_activate_sync (const char *mode);
int appsync_activate_sync_post(const char *mode);
int appsync_mark_active (const gchar *name, int post);
Expand Down
2 changes: 1 addition & 1 deletion src/usb_moded.c
Expand Up @@ -752,7 +752,7 @@ static bool usbmoded_init(void)
}

#ifdef APP_SYNC
appsync_read_list(usbmoded_get_diag_mode());
appsync_read_list();
#endif

/* always read dyn modes even if appsync is not used */
Expand Down

0 comments on commit 89ae6e9

Please sign in to comment.