diff options
author | Richard Hult <rhult@src.gnome.org> | 2003-11-24 14:53:14 +0000 |
---|---|---|
committer | Richard Hult <rhult@src.gnome.org> | 2003-11-24 14:53:14 +0000 |
commit | d90b8a234635e4d1b20096aaaf02f39d725970bb (patch) | |
tree | db2781cc082b9a49e6d0cc196f2270bff93eda8b | |
parent | b8bba414804972003e769048d22eb46e8ddd7324 (diff) | |
download | gconf-d90b8a234635e4d1b20096aaaf02f39d725970bb.tar.gz |
Split out more stuff.
-rw-r--r-- | gconf/gconf-corba-utils.c | 169 | ||||
-rw-r--r-- | gconf/gconf-corba-utils.h | 23 | ||||
-rw-r--r-- | gconf/gconf-internals.c | 475 | ||||
-rw-r--r-- | gconf/gconf-internals.h | 33 |
4 files changed, 188 insertions, 512 deletions
diff --git a/gconf/gconf-corba-utils.c b/gconf/gconf-corba-utils.c index adc3f0f6..99e965c8 100644 --- a/gconf/gconf-corba-utils.c +++ b/gconf/gconf-corba-utils.c @@ -30,6 +30,27 @@ #include <time.h> #include <math.h> +static gchar* daemon_ior = NULL; + +void +gconf_set_daemon_ior(const gchar* ior) +{ + if (daemon_ior != NULL) + { + g_free(daemon_ior); + daemon_ior = NULL; + } + + if (ior != NULL) + daemon_ior = g_strdup(ior); +} + +const gchar* +gconf_get_daemon_ior(void) +{ + return daemon_ior; +} + /* * Locks */ @@ -695,3 +716,151 @@ gconf_CORBA_Object_hash (gconstpointer key) } +/* + * Activation + */ + +static void +set_cloexec (gint fd) +{ + fcntl (fd, F_SETFD, FD_CLOEXEC); +} + +static void +close_fd_func (gpointer data) +{ + int *pipes = data; + + gint open_max; + gint i; + + open_max = sysconf (_SC_OPEN_MAX); + for (i = 3; i < open_max; i++) + { + /* don't close our write pipe */ + if (i != pipes[1]) + set_cloexec (i); + } +} + +ConfigServer +gconf_activate_server (gboolean start_if_not_found, + GError **error) +{ + ConfigServer server; + int p[2] = { -1, -1 }; + char buf[1]; + GError *tmp_err; + char *argv[3]; + char *gconfd_dir; + char *lock_dir; + GString *failure_log; + CORBA_Environment ev; + + failure_log = g_string_new (NULL); + + gconfd_dir = gconf_get_daemon_dir (); + + if (mkdir (gconfd_dir, 0700) < 0 && errno != EEXIST) + gconf_log (GCL_WARNING, _("Failed to create %s: %s"), + gconfd_dir, g_strerror (errno)); + + g_free (gconfd_dir); + + g_string_append (failure_log, " 1: "); + lock_dir = gconf_get_lock_dir (); + server = gconf_get_current_lock_holder (lock_dir, failure_log); + g_free (lock_dir); + + /* Confirm server exists */ + CORBA_exception_init (&ev); + + if (!CORBA_Object_is_nil (server, &ev)) + { + ConfigServer_ping (server, &ev); + + if (ev._major != CORBA_NO_EXCEPTION) + { + server = CORBA_OBJECT_NIL; + + g_string_append_printf (failure_log, + _("Server ping error: %s"), + CORBA_exception_id (&ev)); + } + } + + CORBA_exception_free (&ev); + + if (server != CORBA_OBJECT_NIL) + { + g_string_free (failure_log, TRUE); + return server; + } + + if (start_if_not_found) + { + /* Spawn server */ + if (pipe (p) < 0) + { + g_set_error (error, + GCONF_ERROR, + GCONF_ERROR_NO_SERVER, + _("Failed to create pipe for communicating with spawned gconf daemon: %s\n"), + g_strerror (errno)); + goto out; + } + + argv[0] = g_strconcat (GCONF_SERVERDIR, "/" GCONFD, NULL); + argv[1] = g_strdup_printf ("%d", p[1]); + argv[2] = NULL; + + tmp_err = NULL; + if (!g_spawn_async (NULL, + argv, + NULL, + G_SPAWN_LEAVE_DESCRIPTORS_OPEN, + close_fd_func, + p, + NULL, + &tmp_err)) + { + g_free (argv[0]); + g_free (argv[1]); + g_set_error (error, + GCONF_ERROR, + GCONF_ERROR_NO_SERVER, + _("Failed to launch configuration server: %s\n"), + tmp_err->message); + g_error_free (tmp_err); + goto out; + } + + g_free (argv[0]); + g_free (argv[1]); + + /* Block until server starts up */ + read (p[0], buf, 1); + + g_string_append (failure_log, " 2: "); + lock_dir = gconf_get_lock_dir (); + server = gconf_get_current_lock_holder (lock_dir, failure_log); + g_free (lock_dir); + } + + out: + if (server == CORBA_OBJECT_NIL && + error && + *error == NULL) + g_set_error (error, + GCONF_ERROR, + GCONF_ERROR_NO_SERVER, + _("Failed to contact configuration server; some possible causes are that you need to enable TCP/IP networking for ORBit, or you have stale NFS locks due to a system crash. See http://www.gnome.org/projects/gconf/ for information. (Details - %s)"), + failure_log->len > 0 ? failure_log->str : _("none")); + + g_string_free (failure_log, TRUE); + + close (p[0]); + close (p[1]); + + return server; +} diff --git a/gconf/gconf-corba-utils.h b/gconf/gconf-corba-utils.h index 781ed981..78791698 100644 --- a/gconf/gconf-corba-utils.h +++ b/gconf/gconf-corba-utils.h @@ -23,11 +23,19 @@ #include "GConfX.h" #include "gconf-internals.h" -GConfValue* gconf_value_from_corba_value (const ConfigValue *value); -ConfigValue* gconf_corba_value_from_gconf_value (const GConfValue *value); -void gconf_fill_corba_value_from_gconf_value (const GConfValue *value, - ConfigValue *dest); -ConfigValue* gconf_invalid_corba_value (void); +GConfValue* gconf_value_from_corba_value (const ConfigValue *value); +ConfigValue* gconf_corba_value_from_gconf_value (const GConfValue *value); +void gconf_fill_corba_value_from_gconf_value (const GConfValue *value, + ConfigValue *dest); +ConfigValue* gconf_invalid_corba_value (void); +void gconf_fill_corba_schema_from_gconf_schema (const GConfSchema *sc, + ConfigSchema *dest); +ConfigSchema* gconf_corba_schema_from_gconf_schema (const GConfSchema *sc); +GConfSchema* gconf_schema_from_corba_schema (const ConfigSchema *cs); + + + + void gconf_daemon_blow_away_locks (void); @@ -47,5 +55,10 @@ gboolean gconf_CORBA_Object_equal (gconstpointer a, gconstpointer b); guint gconf_CORBA_Object_hash (gconstpointer key); +ConfigServer gconf_activate_server (gboolean start_if_not_found, + GError **error); + +void gconf_set_daemon_ior (const gchar *ior); +const gchar* gconf_get_daemon_ior (void); #endif diff --git a/gconf/gconf-internals.c b/gconf/gconf-internals.c index aed63caf..46774b08 100644 --- a/gconf/gconf-internals.c +++ b/gconf/gconf-internals.c @@ -41,7 +41,6 @@ gboolean gconf_log_debug_messages = FALSE; static gboolean gconf_daemon_mode = FALSE; -static gchar* daemon_ior = NULL; void gconf_set_daemon_mode(gboolean setting) @@ -55,25 +54,6 @@ gconf_in_daemon_mode(void) return gconf_daemon_mode; } -void -gconf_set_daemon_ior(const gchar* ior) -{ - if (daemon_ior != NULL) - { - g_free(daemon_ior); - daemon_ior = NULL; - } - - if (ior != NULL) - daemon_ior = g_strdup(ior); -} - -const gchar* -gconf_get_daemon_ior(void) -{ - return daemon_ior; -} - gchar* gconf_key_directory (const gchar* key) { @@ -2158,316 +2138,6 @@ gconf_value_encode (GConfValue* val) * file is created atomically using a temporary file, then link() */ -struct _GConfLock { - gchar *lock_directory; - gchar *iorfile; - int lock_fd; -}; - -static void -gconf_lock_destroy (GConfLock* lock) -{ - if (lock->lock_fd >= 0) - close (lock->lock_fd); - g_free (lock->iorfile); - g_free (lock->lock_directory); - g_free (lock); -} - -static void -set_close_on_exec (int fd) -{ - int val; - - val = fcntl (fd, F_GETFD, 0); - if (val < 0) - { - gconf_log (GCL_DEBUG, "couldn't F_GETFD: %s\n", g_strerror (errno)); - return; - } - - val |= FD_CLOEXEC; - - if (fcntl (fd, F_SETFD, val) < 0) - gconf_log (GCL_DEBUG, "couldn't F_SETFD: %s\n", g_strerror (errno)); -} - -/* Your basic Stevens cut-and-paste */ -static int -lock_reg (int fd, int cmd, int type, off_t offset, int whence, off_t len) -{ - struct flock lock; - - lock.l_type = type; /* F_RDLCK, F_WRLCK, F_UNLCK */ - lock.l_start = offset; /* byte offset relative to whence */ - lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */ - lock.l_len = len; /* #bytes, 0 for eof */ - - return fcntl (fd, cmd, &lock); -} - -#define lock_entire_file(fd) \ - lock_reg ((fd), F_SETLK, F_WRLCK, 0, SEEK_SET, 0) -#define unlock_entire_file(fd) \ - lock_reg ((fd), F_SETLK, F_UNLCK, 0, SEEK_SET, 0) - -static gboolean -file_locked_by_someone_else (int fd) -{ - struct flock lock; - - lock.l_type = F_WRLCK; - lock.l_start = 0; - lock.l_whence = SEEK_SET; - lock.l_len = 0; - - if (fcntl (fd, F_GETLK, &lock) < 0) - return TRUE; /* pretend it's locked */ - - if (lock.l_type == F_UNLCK) - return FALSE; /* we have the lock */ - else - return TRUE; /* someone else has it */ -} - -static char* -unique_filename (const char *directory) -{ - char *guid; - char *uniquefile; - - guid = gconf_unique_key (); - uniquefile = g_strconcat (directory, "/", guid, NULL); - g_free (guid); - - return uniquefile; -} - -static int -create_new_locked_file (const gchar *directory, - const gchar *filename, - GError **err) -{ - int fd; - char *uniquefile; - gboolean got_lock; - - got_lock = FALSE; - - uniquefile = unique_filename (directory); - - fd = open (uniquefile, O_WRONLY | O_CREAT, 0700); - - /* Lock our temporary file, lock hopefully applies to the - * inode and so also counts once we link it to the new name - */ - if (lock_entire_file (fd) < 0) - { - g_set_error (err, - GCONF_ERROR, - GCONF_ERROR_LOCK_FAILED, - _("Could not lock temporary file '%s': %s"), - uniquefile, g_strerror (errno)); - goto out; - } - - /* Create lockfile as a link to unique file */ - if (link (uniquefile, filename) == 0) - { - /* filename didn't exist before, and open succeeded, and we have the lock */ - got_lock = TRUE; - goto out; - } - else - { - /* see if the link really succeeded */ - struct stat sb; - if (stat (uniquefile, &sb) == 0 && - sb.st_nlink == 2) - { - got_lock = TRUE; - goto out; - } - else - { - g_set_error (err, - GCONF_ERROR, - GCONF_ERROR_LOCK_FAILED, - _("Could not create file '%s', probably because it already exists"), - filename); - goto out; - } - } - - out: - if (got_lock) - set_close_on_exec (fd); - - unlink (uniquefile); - g_free (uniquefile); - - if (!got_lock) - { - if (fd >= 0) - close (fd); - fd = -1; - } - - return fd; -} - -static int -open_empty_locked_file (const gchar *directory, - const gchar *filename, - GError **err) -{ - int fd; - - fd = create_new_locked_file (directory, filename, NULL); - - if (fd >= 0) - return fd; - - /* We failed to create the file, most likely because it already - * existed; try to get the lock on the existing file, and if we can - * get that lock, delete it, then start over. - */ - fd = open (filename, O_RDWR, 0700); - if (fd < 0) - { - /* File has gone away? */ - g_set_error (err, - GCONF_ERROR, - GCONF_ERROR_LOCK_FAILED, - _("Failed to create or open '%s'"), - filename); - return -1; - } - - if (lock_entire_file (fd) < 0) - { - g_set_error (err, - GCONF_ERROR, - GCONF_ERROR_LOCK_FAILED, - _("Failed to lock '%s': probably another process has the lock, or your operating system has NFS file locking misconfigured (%s)"), - filename, strerror (errno)); - close (fd); - return -1; - } - - /* We have the lock on filename, so delete it */ - /* FIXME this leaves .nfs32423432 cruft */ - unlink (filename); - close (fd); - fd = -1; - - /* Now retry creating our file */ - fd = create_new_locked_file (directory, filename, err); - - return fd; -} - -static ConfigServer -read_current_server_and_set_warning (const gchar *iorfile, - GString *warning) -{ - FILE *fp; - - fp = fopen (iorfile, "r"); - - if (fp == NULL) - { - if (warning) - g_string_append_printf (warning, - _("IOR file '%s' not opened successfully, no gconfd located: %s"), - iorfile, g_strerror (errno)); - - return CORBA_OBJECT_NIL; - } - else /* successfully opened IOR file */ - { - char buf[2048] = { '\0' }; - const char *str = NULL; - - fgets (buf, sizeof (buf) - 2, fp); - fclose (fp); - - /* The lockfile format is <pid>:<ior> for gconfd - * or <pid>:none for gconftool - */ - str = buf; - while (isdigit ((unsigned char) *str)) - ++str; - - if (*str == ':') - ++str; - - if (str[0] == 'n' && - str[1] == 'o' && - str[2] == 'n' && - str[3] == 'e') - { - if (warning) - g_string_append_printf (warning, - _("gconftool or other non-gconfd process has the lock file '%s'"), - iorfile); - } - else /* file contains daemon IOR */ - { - CORBA_ORB orb; - CORBA_Environment ev; - ConfigServer server; - - CORBA_exception_init (&ev); - - orb = gconf_orb_get (); - - if (orb == NULL) - { - if (warning) - g_string_append_printf (warning, - _("couldn't contact ORB to resolve existing gconfd object reference")); - return CORBA_OBJECT_NIL; - } - - server = CORBA_ORB_string_to_object (orb, (char*) str, &ev); - CORBA_exception_free (&ev); - - if (server == CORBA_OBJECT_NIL && - warning) - g_string_append_printf (warning, - _("Failed to convert IOR '%s' to an object reference"), - str); - - return server; - } - - return CORBA_OBJECT_NIL; - } -} - -static ConfigServer -read_current_server (const gchar *iorfile, - gboolean warn_if_fail) -{ - GString *warning; - ConfigServer server; - - if (warn_if_fail) - warning = g_string_new (NULL); - else - warning = NULL; - - server = read_current_server_and_set_warning (iorfile, warning); - - if (warning->len > 0) - gconf_log (GCL_WARNING, "%s", warning->str); - - g_string_free (warning, TRUE); - - return server; -} - char* gconf_get_daemon_dir (void) { @@ -2501,151 +2171,6 @@ gconf_get_lock_dir (void) return lock_dir; } -static void -set_cloexec (gint fd) -{ - fcntl (fd, F_SETFD, FD_CLOEXEC); -} - -static void -close_fd_func (gpointer data) -{ - int *pipes = data; - - gint open_max; - gint i; - - open_max = sysconf (_SC_OPEN_MAX); - for (i = 3; i < open_max; i++) - { - /* don't close our write pipe */ - if (i != pipes[1]) - set_cloexec (i); - } -} - -ConfigServer -gconf_activate_server (gboolean start_if_not_found, - GError **error) -{ - ConfigServer server; - int p[2] = { -1, -1 }; - char buf[1]; - GError *tmp_err; - char *argv[3]; - char *gconfd_dir; - char *lock_dir; - GString *failure_log; - CORBA_Environment ev; - - failure_log = g_string_new (NULL); - - gconfd_dir = gconf_get_daemon_dir (); - - if (mkdir (gconfd_dir, 0700) < 0 && errno != EEXIST) - gconf_log (GCL_WARNING, _("Failed to create %s: %s"), - gconfd_dir, g_strerror (errno)); - - g_free (gconfd_dir); - - g_string_append (failure_log, " 1: "); - lock_dir = gconf_get_lock_dir (); - server = gconf_get_current_lock_holder (lock_dir, failure_log); - g_free (lock_dir); - - /* Confirm server exists */ - CORBA_exception_init (&ev); - - if (!CORBA_Object_is_nil (server, &ev)) - { - ConfigServer_ping (server, &ev); - - if (ev._major != CORBA_NO_EXCEPTION) - { - server = CORBA_OBJECT_NIL; - - g_string_append_printf (failure_log, - _("Server ping error: %s"), - CORBA_exception_id (&ev)); - } - } - - CORBA_exception_free (&ev); - - if (server != CORBA_OBJECT_NIL) - { - g_string_free (failure_log, TRUE); - return server; - } - - if (start_if_not_found) - { - /* Spawn server */ - if (pipe (p) < 0) - { - g_set_error (error, - GCONF_ERROR, - GCONF_ERROR_NO_SERVER, - _("Failed to create pipe for communicating with spawned gconf daemon: %s\n"), - g_strerror (errno)); - goto out; - } - - argv[0] = g_strconcat (GCONF_SERVERDIR, "/" GCONFD, NULL); - argv[1] = g_strdup_printf ("%d", p[1]); - argv[2] = NULL; - - tmp_err = NULL; - if (!g_spawn_async (NULL, - argv, - NULL, - G_SPAWN_LEAVE_DESCRIPTORS_OPEN, - close_fd_func, - p, - NULL, - &tmp_err)) - { - g_free (argv[0]); - g_free (argv[1]); - g_set_error (error, - GCONF_ERROR, - GCONF_ERROR_NO_SERVER, - _("Failed to launch configuration server: %s\n"), - tmp_err->message); - g_error_free (tmp_err); - goto out; - } - - g_free (argv[0]); - g_free (argv[1]); - - /* Block until server starts up */ - read (p[0], buf, 1); - - g_string_append (failure_log, " 2: "); - lock_dir = gconf_get_lock_dir (); - server = gconf_get_current_lock_holder (lock_dir, failure_log); - g_free (lock_dir); - } - - out: - if (server == CORBA_OBJECT_NIL && - error && - *error == NULL) - g_set_error (error, - GCONF_ERROR, - GCONF_ERROR_NO_SERVER, - _("Failed to contact configuration server; some possible causes are that you need to enable TCP/IP networking for ORBit, or you have stale NFS locks due to a system crash. See http://www.gnome.org/projects/gconf/ for information. (Details - %s)"), - failure_log->len > 0 ? failure_log->str : _("none")); - - g_string_free (failure_log, TRUE); - - close (p[0]); - close (p[1]); - - return server; -} - void _gconf_init_i18n (void) { diff --git a/gconf/gconf-internals.h b/gconf/gconf-internals.h index fbb1b375..9eaacab9 100644 --- a/gconf/gconf-internals.h +++ b/gconf/gconf-internals.h @@ -38,8 +38,6 @@ #include "gconf-engine.h" #include "gconf-sources.h" -#include "GConfX.h" - gchar* gconf_key_directory (const gchar* key); const gchar* gconf_key_key (const gchar* key); @@ -54,17 +52,6 @@ enum { gboolean gconf_file_test (const gchar* filename, int test); gboolean gconf_file_exists (const gchar* filename); -GConfValue* gconf_value_from_corba_value (const ConfigValue *value); -ConfigValue* gconf_corba_value_from_gconf_value (const GConfValue *value); -void gconf_fill_corba_value_from_gconf_value (const GConfValue *value, - ConfigValue *dest); -ConfigValue* gconf_invalid_corba_value (void); - -void gconf_fill_corba_schema_from_gconf_schema (const GConfSchema *sc, - ConfigSchema *dest); -ConfigSchema* gconf_corba_schema_from_gconf_schema (const GConfSchema *sc); -GConfSchema* gconf_schema_from_corba_schema (const ConfigSchema *cs); - const gchar* gconf_value_type_to_string (GConfValueType type); GConfValueType gconf_value_type_from_string (const gchar *str); @@ -137,21 +124,11 @@ void gconf_unquote_string_inplace (gchar *str, GConfValue* gconf_value_decode (const gchar *encoded); gchar* gconf_value_encode (GConfValue *val); -GConfValue* gconf_value_from_corba_value (const ConfigValue *value); -ConfigValue* gconf_corba_value_from_gconf_value (const GConfValue *value); -void gconf_fill_corba_value_from_gconf_value (const GConfValue *value, - ConfigValue *dest); -ConfigValue* gconf_invalid_corba_value (void); + /* FIXME is this used? */ gchar* gconf_quote_percents (const gchar* src); -gchar* gconf_object_to_string (CORBA_Object obj, - GError **err); -gboolean gconf_CORBA_Object_equal (gconstpointer a, - gconstpointer b); -guint gconf_CORBA_Object_hash (gconstpointer key); - /* @@ -180,11 +157,6 @@ gboolean gconf_value_pair_to_primitive_pair_destructive (GConfValue *val, void gconf_set_daemon_mode (gboolean setting); gboolean gconf_in_daemon_mode (void); -void gconf_set_daemon_ior (const gchar *ior); -const gchar* gconf_get_daemon_ior (void); - -/* Returns TRUE if there was an error, frees exception, sets err */ -gboolean gconf_handle_oaf_exception (CORBA_Environment* ev, GError** err); void gconf_nanosleep (gulong useconds); @@ -200,9 +172,6 @@ void gconf_set_error (GError** err, /* merge two errors into a single message */ GError* gconf_compose_errors (GError* err1, GError* err2); -ConfigServer gconf_activate_server (gboolean start_if_not_found, - GError **error); - char* gconf_get_lock_dir (void); char* gconf_get_daemon_dir (void); |