2019-09-10 11:19:01 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2019-09-25 13:13:40 +02:00
|
|
|
/*
|
2012-06-05 11:09:05 -05:00
|
|
|
* Copyright (C) 2008 - 2012 Red Hat, Inc.
|
2008-04-27 14:30:06 +00:00
|
|
|
*/
|
|
|
|
|
|
2016-02-12 14:44:52 +01:00
|
|
|
#include "nm-default.h"
|
|
|
|
|
|
2008-04-27 14:30:06 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/wait.h>
|
2008-08-13 11:45:28 +00:00
|
|
|
#include <arpa/inet.h>
|
2013-11-18 23:37:58 +01:00
|
|
|
#include <glib-unix.h>
|
2008-04-27 14:30:06 +00:00
|
|
|
|
shared: build helper "libnm-libnm-core-{intern|aux}.la" library for libnm-core
"libnm-core" implements common functionality for "NetworkManager" and
"libnm".
Note that clients like "nmcli" cannot access the internal API provided
by "libnm-core". So, if nmcli wants to do something that is also done by
"libnm-core", , "libnm", or "NetworkManager", the code would have to be
duplicated.
Instead, such code can be in "libnm-libnm-core-{intern|aux}.la".
Note that:
0) "libnm-libnm-core-intern.la" is used by libnm-core itsself.
On the other hand, "libnm-libnm-core-aux.la" is not used by
libnm-core, but provides utilities on top of it.
1) they both extend "libnm-core" with utlities that are not public
API of libnm itself. Maybe part of the code should one day become
public API of libnm. On the other hand, this is code for which
we may not want to commit to a stable interface or which we
don't want to provide as part of the API.
2) "libnm-libnm-core-intern.la" is statically linked by "libnm-core"
and thus directly available to "libnm" and "NetworkManager".
On the other hand, "libnm-libnm-core-aux.la" may be used by "libnm"
and "NetworkManager".
Both libraries may be statically linked by libnm clients (like
nmcli).
3) it must only use glib, libnm-glib-aux.la, and the public API
of libnm-core.
This is important: it must not use "libnm-core/nm-core-internal.h"
nor "libnm-core/nm-utils-private.h" so the static library is usable
by nmcli which couldn't access these.
Note that "shared/nm-meta-setting.c" is an entirely different case,
because it behaves differently depending on whether linking against
"libnm-core" or the client programs. As such, this file must be compiled
twice.
(cherry picked from commit af07ed01c04867e281cc3982a7ab0d244d4f8e2e)
2019-04-15 09:26:53 +02:00
|
|
|
#include "nm-libnm-core-aux/nm-dispatcher-api.h"
|
2011-05-02 22:38:51 -05:00
|
|
|
#include "nm-dispatcher-utils.h"
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2019-05-23 10:59:26 +02:00
|
|
|
/*****************************************************************************/
|
2014-09-03 12:27:33 -04:00
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
typedef struct Request Request;
|
|
|
|
|
|
2019-05-23 10:47:46 +02:00
|
|
|
static struct {
|
2019-05-23 11:27:33 +02:00
|
|
|
GDBusConnection *dbus_connection;
|
2019-05-23 10:47:46 +02:00
|
|
|
GMainLoop *loop;
|
|
|
|
|
gboolean debug;
|
|
|
|
|
gboolean persist;
|
|
|
|
|
guint quit_id;
|
|
|
|
|
guint request_id_counter;
|
2019-05-23 10:58:35 +02:00
|
|
|
gboolean ever_acquired_name;
|
2019-05-23 11:18:48 +02:00
|
|
|
bool exit_with_failure;
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2014-03-31 09:04:46 -04:00
|
|
|
Request *current_request;
|
2015-07-14 18:17:33 +02:00
|
|
|
GQueue *requests_waiting;
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
int num_requests_pending;
|
2019-05-23 11:45:20 +02:00
|
|
|
} gl;
|
2008-04-27 14:30:06 +00:00
|
|
|
|
|
|
|
|
typedef struct {
|
2012-06-05 11:09:05 -05:00
|
|
|
Request *request;
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2012-06-05 11:09:05 -05:00
|
|
|
char *script;
|
|
|
|
|
GPid pid;
|
|
|
|
|
DispatchResult result;
|
|
|
|
|
char *error;
|
2015-07-14 18:17:33 +02:00
|
|
|
gboolean wait;
|
|
|
|
|
gboolean dispatched;
|
|
|
|
|
guint watch_id;
|
|
|
|
|
guint timeout_id;
|
2012-06-05 11:09:05 -05:00
|
|
|
} ScriptInfo;
|
|
|
|
|
|
|
|
|
|
struct Request {
|
2015-08-03 15:52:11 +02:00
|
|
|
guint request_id;
|
|
|
|
|
|
2014-09-03 12:27:33 -04:00
|
|
|
GDBusMethodInvocation *context;
|
2012-06-05 11:09:05 -05:00
|
|
|
char *action;
|
|
|
|
|
char *iface;
|
|
|
|
|
char **envp;
|
2014-03-31 14:26:28 -04:00
|
|
|
gboolean debug;
|
|
|
|
|
|
2012-06-05 11:09:05 -05:00
|
|
|
GPtrArray *scripts; /* list of ScriptInfo */
|
|
|
|
|
guint idx;
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
int num_scripts_done;
|
|
|
|
|
int num_scripts_nowait;
|
2012-06-05 11:09:05 -05:00
|
|
|
};
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2015-08-03 15:52:11 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2019-05-23 09:46:47 +02:00
|
|
|
#define __LOG_print(print_cmd, ...) \
|
2015-08-03 15:52:11 +02:00
|
|
|
G_STMT_START { \
|
2019-05-23 09:46:47 +02:00
|
|
|
if (FALSE) { \
|
|
|
|
|
/* g_message() alone does not warn about invalid format. Add a dummy printf() statement to
|
|
|
|
|
* get a compiler warning about wrong format. */ \
|
|
|
|
|
printf (__VA_ARGS__); \
|
|
|
|
|
} \
|
|
|
|
|
print_cmd (__VA_ARGS__); \
|
|
|
|
|
} G_STMT_END
|
|
|
|
|
|
|
|
|
|
#define __LOG_print_R(print_cmd, _request, ...) \
|
|
|
|
|
G_STMT_START { \
|
|
|
|
|
__LOG_print (print_cmd, \
|
|
|
|
|
"req:%u '%s'%s%s%s" _NM_UTILS_MACRO_FIRST (__VA_ARGS__), \
|
|
|
|
|
(_request)->request_id, \
|
|
|
|
|
(_request)->action, \
|
|
|
|
|
(_request)->iface ? " [" : "", \
|
|
|
|
|
(_request)->iface ?: "", \
|
|
|
|
|
(_request)->iface ? "]" : "" \
|
|
|
|
|
_NM_UTILS_MACRO_REST (__VA_ARGS__)); \
|
|
|
|
|
} G_STMT_END
|
|
|
|
|
|
|
|
|
|
#define __LOG_print_S(print_cmd, _request, _script, ...) \
|
|
|
|
|
G_STMT_START { \
|
|
|
|
|
__LOG_print_R (print_cmd, \
|
|
|
|
|
(_request), \
|
|
|
|
|
"%s%s%s" _NM_UTILS_MACRO_FIRST (__VA_ARGS__), \
|
|
|
|
|
(_script) ? ", \"" : "", \
|
|
|
|
|
(_script) ? (_script)->script : "", \
|
|
|
|
|
(_script) ? "\"" : "" \
|
|
|
|
|
_NM_UTILS_MACRO_REST (__VA_ARGS__)); \
|
|
|
|
|
} G_STMT_END
|
|
|
|
|
|
|
|
|
|
#define _LOG_X_(enabled_cmd, print_cmd, ...) \
|
|
|
|
|
G_STMT_START { \
|
|
|
|
|
if (enabled_cmd) \
|
|
|
|
|
__LOG_print (print_cmd, __VA_ARGS__); \
|
2015-08-03 15:52:11 +02:00
|
|
|
} G_STMT_END
|
|
|
|
|
|
2019-05-23 09:46:47 +02:00
|
|
|
#define _LOG_R_(enabled_cmd, x_request, print_cmd, ...) \
|
2015-08-03 15:52:11 +02:00
|
|
|
G_STMT_START { \
|
2019-05-23 09:46:47 +02:00
|
|
|
const Request *const _request = (x_request); \
|
2015-08-03 15:52:11 +02:00
|
|
|
\
|
2019-05-23 09:46:47 +02:00
|
|
|
nm_assert (_request); \
|
|
|
|
|
if (enabled_cmd) \
|
|
|
|
|
__LOG_print_R (print_cmd, _request, ": "__VA_ARGS__); \
|
2015-08-03 15:52:11 +02:00
|
|
|
} G_STMT_END
|
|
|
|
|
|
2019-05-23 09:46:47 +02:00
|
|
|
#define _LOG_S_(enabled_cmd, x_script, print_cmd, ...) \
|
|
|
|
|
G_STMT_START { \
|
|
|
|
|
const ScriptInfo *const _script = (x_script); \
|
|
|
|
|
const Request *const _request = _script ? _script->request : NULL; \
|
|
|
|
|
\
|
|
|
|
|
nm_assert (_script && _request); \
|
|
|
|
|
if (enabled_cmd) \
|
|
|
|
|
__LOG_print_S (print_cmd, _request, _script, ": "__VA_ARGS__); \
|
|
|
|
|
} G_STMT_END
|
|
|
|
|
|
2019-05-23 10:47:46 +02:00
|
|
|
#define _LOG_X_D_enabled() (gl.debug)
|
2019-05-23 09:46:47 +02:00
|
|
|
#define _LOG_X_T_enabled() _LOG_X_D_enabled ()
|
|
|
|
|
|
|
|
|
|
#define _LOG_R_D_enabled(request) (_NM_ENSURE_TYPE_CONST (Request *, request)->debug)
|
2019-05-23 09:10:07 +02:00
|
|
|
#define _LOG_R_T_enabled(request) _LOG_R_D_enabled (request)
|
2015-08-03 15:52:11 +02:00
|
|
|
|
2019-05-23 09:46:47 +02:00
|
|
|
#define _LOG_X_T(...) _LOG_X_ (_LOG_X_T_enabled (), g_debug, __VA_ARGS__)
|
|
|
|
|
#define _LOG_X_D(...) _LOG_X_ (_LOG_X_D_enabled (), g_info, __VA_ARGS__)
|
|
|
|
|
#define _LOG_X_I(...) _LOG_X_ (TRUE, g_message, __VA_ARGS__)
|
|
|
|
|
#define _LOG_X_W(...) _LOG_X_ (TRUE, g_warning, __VA_ARGS__)
|
|
|
|
|
|
|
|
|
|
#define _LOG_R_T(request, ...) _LOG_R_ (_LOG_R_T_enabled (_request), request, g_debug, __VA_ARGS__)
|
|
|
|
|
#define _LOG_R_D(request, ...) _LOG_R_ (_LOG_R_D_enabled (_request), request, g_info, __VA_ARGS__)
|
|
|
|
|
#define _LOG_R_W(request, ...) _LOG_R_ (TRUE, request, g_warning, __VA_ARGS__)
|
2015-08-03 15:52:11 +02:00
|
|
|
|
2019-05-23 09:46:47 +02:00
|
|
|
#define _LOG_S_T(script, ...) _LOG_S_ (_LOG_R_T_enabled (_request), script, g_debug, __VA_ARGS__)
|
|
|
|
|
#define _LOG_S_D(script, ...) _LOG_S_ (_LOG_R_D_enabled (_request), script, g_info, __VA_ARGS__)
|
|
|
|
|
#define _LOG_S_W(script, ...) _LOG_S_ (TRUE, script, g_warning, __VA_ARGS__)
|
2015-08-03 15:52:11 +02:00
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
static gboolean dispatch_one_script (Request *request);
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2012-06-05 11:09:05 -05:00
|
|
|
static void
|
2013-10-18 16:07:26 +02:00
|
|
|
script_info_free (gpointer ptr)
|
2012-06-05 11:09:05 -05:00
|
|
|
{
|
2013-10-18 16:07:26 +02:00
|
|
|
ScriptInfo *info = ptr;
|
|
|
|
|
|
2012-06-05 11:09:05 -05:00
|
|
|
g_free (info->script);
|
|
|
|
|
g_free (info->error);
|
2015-08-03 16:15:25 +02:00
|
|
|
g_slice_free (ScriptInfo, info);
|
2012-06-05 11:09:05 -05:00
|
|
|
}
|
2008-04-27 14:30:06 +00:00
|
|
|
|
|
|
|
|
static void
|
2012-06-05 11:09:05 -05:00
|
|
|
request_free (Request *request)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
2015-07-14 18:17:33 +02:00
|
|
|
g_assert_cmpuint (request->num_scripts_done, ==, request->scripts->len);
|
2015-07-27 22:44:56 +02:00
|
|
|
g_assert_cmpuint (request->num_scripts_nowait, ==, 0);
|
2015-07-14 18:17:33 +02:00
|
|
|
|
2012-06-05 11:09:05 -05:00
|
|
|
g_free (request->action);
|
|
|
|
|
g_free (request->iface);
|
|
|
|
|
g_strfreev (request->envp);
|
2016-02-04 16:51:36 +01:00
|
|
|
g_ptr_array_free (request->scripts, TRUE);
|
2015-07-14 18:17:33 +02:00
|
|
|
|
2015-08-03 16:15:25 +02:00
|
|
|
g_slice_free (Request, request);
|
2012-06-05 11:09:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
quit_timeout_cb (gpointer user_data)
|
|
|
|
|
{
|
2019-05-23 10:49:06 +02:00
|
|
|
gl.quit_id = 0;
|
2019-05-23 10:47:46 +02:00
|
|
|
g_main_loop_quit (gl.loop);
|
2019-05-23 10:49:06 +02:00
|
|
|
return G_SOURCE_REMOVE;
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-31 08:38:06 -04:00
|
|
|
static void
|
2014-09-03 12:27:33 -04:00
|
|
|
quit_timeout_reschedule (void)
|
2014-03-31 08:38:06 -04:00
|
|
|
{
|
2019-05-23 10:47:46 +02:00
|
|
|
if (!gl.persist) {
|
|
|
|
|
nm_clear_g_source (&gl.quit_id);
|
|
|
|
|
gl.quit_id = g_timeout_add_seconds (10, quit_timeout_cb, NULL);
|
2015-07-14 18:17:33 +02:00
|
|
|
}
|
2012-06-05 11:09:05 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
/**
|
|
|
|
|
* next_request:
|
|
|
|
|
*
|
|
|
|
|
* @request: (allow-none): the request to set as next. If %NULL, dequeue the next
|
|
|
|
|
* waiting request. Otherwise, try to set the given request.
|
|
|
|
|
*
|
|
|
|
|
* Sets the currently active request (@current_request). The current request
|
|
|
|
|
* is a request that has at least on "wait" script, because requests that only
|
|
|
|
|
* consist of "no-wait" scripts are handled right away and not enqueued to
|
|
|
|
|
* @requests_waiting nor set as @current_request.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE, if there was currently not request in process and it set
|
|
|
|
|
* a new request as current.
|
|
|
|
|
*/
|
|
|
|
|
static gboolean
|
2019-05-23 11:45:20 +02:00
|
|
|
next_request (Request *request)
|
2014-03-31 09:04:46 -04:00
|
|
|
{
|
2015-07-14 18:17:33 +02:00
|
|
|
if (request) {
|
2019-05-23 11:45:20 +02:00
|
|
|
if (gl.current_request) {
|
|
|
|
|
g_queue_push_tail (gl.requests_waiting, request);
|
2015-07-14 18:17:33 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* when calling next_request() without explicit @request, we always
|
|
|
|
|
* forcefully clear @current_request. That one is certainly
|
|
|
|
|
* handled already. */
|
2019-05-23 11:45:20 +02:00
|
|
|
gl.current_request = NULL;
|
2015-07-14 18:17:33 +02:00
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
request = g_queue_pop_head (gl.requests_waiting);
|
2015-07-14 18:17:33 +02:00
|
|
|
if (!request)
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 09:24:19 +02:00
|
|
|
_LOG_R_D (request, "start running ordered scripts...");
|
2014-03-31 14:26:28 -04:00
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
gl.current_request = request;
|
2014-03-31 09:04:46 -04:00
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
return TRUE;
|
2014-03-31 09:04:46 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
/**
|
|
|
|
|
* complete_request:
|
|
|
|
|
* @request: the request
|
|
|
|
|
*
|
|
|
|
|
* Checks if all the scripts for the request have terminated and in such case
|
|
|
|
|
* it sends the D-Bus response and releases the request resources.
|
|
|
|
|
*
|
|
|
|
|
* It also decreases @num_requests_pending and possibly does quit_timeout_reschedule().
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
complete_request (Request *request)
|
2012-06-05 11:09:05 -05:00
|
|
|
{
|
2014-09-03 12:27:33 -04:00
|
|
|
GVariantBuilder results;
|
|
|
|
|
GVariant *ret;
|
2012-06-05 11:09:05 -05:00
|
|
|
guint i;
|
|
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
nm_assert (request);
|
|
|
|
|
|
|
|
|
|
/* Are there still pending scripts? Then do nothing (for now). */
|
|
|
|
|
if (request->num_scripts_done < request->scripts->len)
|
|
|
|
|
return;
|
2012-06-05 11:09:05 -05:00
|
|
|
|
2014-09-03 12:27:33 -04:00
|
|
|
g_variant_builder_init (&results, G_VARIANT_TYPE ("a(sus)"));
|
2012-06-05 11:09:05 -05:00
|
|
|
for (i = 0; i < request->scripts->len; i++) {
|
|
|
|
|
ScriptInfo *script = g_ptr_array_index (request->scripts, i);
|
|
|
|
|
|
2014-09-03 12:27:33 -04:00
|
|
|
g_variant_builder_add (&results, "(sus)",
|
|
|
|
|
script->script,
|
|
|
|
|
script->result,
|
2018-04-24 11:20:03 +02:00
|
|
|
script->error ?: "");
|
2012-06-05 11:09:05 -05:00
|
|
|
}
|
|
|
|
|
|
2014-09-03 12:27:33 -04:00
|
|
|
ret = g_variant_new ("(a(sus))", &results);
|
|
|
|
|
g_dbus_method_invocation_return_value (request->context, ret);
|
2014-03-31 14:26:28 -04:00
|
|
|
|
2019-05-23 09:24:19 +02:00
|
|
|
_LOG_R_T (request, "completed (%u scripts)", request->scripts->len);
|
2015-07-14 18:17:33 +02:00
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
if (gl.current_request == request)
|
|
|
|
|
gl.current_request = NULL;
|
2015-07-14 18:17:33 +02:00
|
|
|
|
2012-06-05 11:09:05 -05:00
|
|
|
request_free (request);
|
2014-03-31 08:38:06 -04:00
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
g_assert_cmpuint (gl.num_requests_pending, >, 0);
|
|
|
|
|
if (--gl.num_requests_pending <= 0) {
|
|
|
|
|
nm_assert (!gl.current_request && !g_queue_peek_head (gl.requests_waiting));
|
2015-07-14 18:17:33 +02:00
|
|
|
quit_timeout_reschedule ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
complete_script (ScriptInfo *script)
|
|
|
|
|
{
|
2016-01-12 16:06:21 +01:00
|
|
|
Request *request;
|
2015-07-14 18:17:33 +02:00
|
|
|
gboolean wait = script->wait;
|
|
|
|
|
|
2016-01-12 16:06:21 +01:00
|
|
|
request = script->request;
|
|
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
if (wait) {
|
|
|
|
|
/* for "wait" scripts, try to schedule the next blocking script.
|
|
|
|
|
* If that is successful, return (as we must wait for its completion). */
|
2016-01-12 16:06:21 +01:00
|
|
|
if (dispatch_one_script (request))
|
2015-07-14 18:17:33 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
nm_assert (!wait || gl.current_request == request);
|
2015-07-14 18:17:33 +02:00
|
|
|
|
2016-01-12 16:06:21 +01:00
|
|
|
/* Try to complete the request. @request will be possibly free'd,
|
|
|
|
|
* making @script and @request a dangling pointer. */
|
|
|
|
|
complete_request (request);
|
2015-07-14 18:17:33 +02:00
|
|
|
|
|
|
|
|
if (!wait) {
|
|
|
|
|
/* this was a "no-wait" script. We either completed the request,
|
|
|
|
|
* or there is nothing to do. Especially, there is no need to
|
|
|
|
|
* queue the next_request() -- because no-wait scripts don't block
|
2015-07-27 22:44:56 +02:00
|
|
|
* requests. However, if this was the last "no-wait" script and
|
|
|
|
|
* there are "wait" scripts ready to run, launch them.
|
|
|
|
|
*/
|
2019-05-23 11:45:20 +02:00
|
|
|
if ( gl.current_request == request
|
|
|
|
|
&& gl.current_request->num_scripts_nowait == 0) {
|
2015-07-27 22:44:56 +02:00
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
if (dispatch_one_script (gl.current_request))
|
2015-07-27 22:44:56 +02:00
|
|
|
return;
|
|
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
complete_request (gl.current_request);
|
2015-07-27 22:44:56 +02:00
|
|
|
} else
|
|
|
|
|
return;
|
2016-01-12 16:08:46 +01:00
|
|
|
} else {
|
|
|
|
|
/* if the script is a "wait" script, we already tried above to
|
|
|
|
|
* dispatch the next script. As we didn't do that, it means we
|
|
|
|
|
* just completed the last script of @request and we can continue
|
|
|
|
|
* with the next request...
|
|
|
|
|
*
|
|
|
|
|
* Also, it cannot be that there is another request currently being
|
|
|
|
|
* processed because only requests with "wait" scripts can become
|
|
|
|
|
* @current_request. As there can only be one "wait" script running
|
|
|
|
|
* at any time, it means complete_request() above completed @request. */
|
2019-05-23 11:45:20 +02:00
|
|
|
nm_assert (!gl.current_request);
|
2015-07-14 18:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
while (next_request (NULL)) {
|
|
|
|
|
request = gl.current_request;
|
2015-07-14 18:17:33 +02:00
|
|
|
|
|
|
|
|
if (dispatch_one_script (request))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Try to complete the request. It will be either completed
|
|
|
|
|
* now, or when all pending "no-wait" scripts return. */
|
|
|
|
|
complete_request (request);
|
|
|
|
|
|
|
|
|
|
/* We can immediately start next_request(), because our current
|
|
|
|
|
* @request has obviously no more "wait" scripts either.
|
|
|
|
|
* Repeat... */
|
|
|
|
|
}
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
script_watch_cb (GPid pid, int status, gpointer user_data)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
2012-06-05 11:09:05 -05:00
|
|
|
ScriptInfo *script = user_data;
|
|
|
|
|
guint err;
|
|
|
|
|
|
|
|
|
|
g_assert (pid == script->pid);
|
|
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
script->watch_id = 0;
|
|
|
|
|
nm_clear_g_source (&script->timeout_id);
|
|
|
|
|
script->request->num_scripts_done++;
|
2015-07-27 22:44:56 +02:00
|
|
|
if (!script->wait)
|
|
|
|
|
script->request->num_scripts_nowait--;
|
2012-06-05 11:09:05 -05:00
|
|
|
|
|
|
|
|
if (WIFEXITED (status)) {
|
|
|
|
|
err = WEXITSTATUS (status);
|
|
|
|
|
if (err == 0)
|
|
|
|
|
script->result = DISPATCH_RESULT_SUCCESS;
|
|
|
|
|
else {
|
|
|
|
|
script->error = g_strdup_printf ("Script '%s' exited with error status %d.",
|
|
|
|
|
script->script, err);
|
|
|
|
|
}
|
|
|
|
|
} else if (WIFSTOPPED (status)) {
|
|
|
|
|
script->error = g_strdup_printf ("Script '%s' stopped unexpectedly with signal %d.",
|
|
|
|
|
script->script, WSTOPSIG (status));
|
|
|
|
|
} else if (WIFSIGNALED (status)) {
|
|
|
|
|
script->error = g_strdup_printf ("Script '%s' died with signal %d",
|
|
|
|
|
script->script, WTERMSIG (status));
|
|
|
|
|
} else {
|
|
|
|
|
script->error = g_strdup_printf ("Script '%s' died from an unknown cause",
|
|
|
|
|
script->script);
|
|
|
|
|
}
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2014-03-31 14:26:28 -04:00
|
|
|
if (script->result == DISPATCH_RESULT_SUCCESS) {
|
2019-05-23 09:24:19 +02:00
|
|
|
_LOG_S_T (script, "complete");
|
2014-03-31 14:26:28 -04:00
|
|
|
} else {
|
2012-06-05 11:09:05 -05:00
|
|
|
script->result = DISPATCH_RESULT_FAILED;
|
2015-08-03 15:52:11 +02:00
|
|
|
_LOG_S_W (script, "complete: failed with %s", script->error);
|
2012-06-05 11:09:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_spawn_close_pid (script->pid);
|
2015-07-14 18:17:33 +02:00
|
|
|
|
|
|
|
|
complete_script (script);
|
2012-06-05 11:09:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
script_timeout_cb (gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
ScriptInfo *script = user_data;
|
|
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
script->timeout_id = 0;
|
|
|
|
|
nm_clear_g_source (&script->watch_id);
|
|
|
|
|
script->request->num_scripts_done++;
|
2015-07-27 22:44:56 +02:00
|
|
|
if (!script->wait)
|
|
|
|
|
script->request->num_scripts_nowait--;
|
2012-06-05 11:09:05 -05:00
|
|
|
|
2015-08-03 15:52:11 +02:00
|
|
|
_LOG_S_W (script, "complete: timeout (kill script)");
|
2012-06-05 11:09:05 -05:00
|
|
|
|
2015-05-03 13:57:16 +02:00
|
|
|
kill (script->pid, SIGKILL);
|
|
|
|
|
again:
|
|
|
|
|
if (waitpid (script->pid, NULL, 0) == -1) {
|
|
|
|
|
if (errno == EINTR)
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
2012-06-05 11:09:05 -05:00
|
|
|
|
|
|
|
|
script->error = g_strdup_printf ("Script '%s' timed out.", script->script);
|
|
|
|
|
script->result = DISPATCH_RESULT_TIMEOUT;
|
|
|
|
|
|
|
|
|
|
g_spawn_close_pid (script->pid);
|
2015-07-14 18:17:33 +02:00
|
|
|
|
|
|
|
|
complete_script (script);
|
|
|
|
|
|
2012-06-05 11:09:05 -05:00
|
|
|
return FALSE;
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 09:04:23 +01:00
|
|
|
static gboolean
|
2014-06-03 15:58:23 -05:00
|
|
|
check_permissions (struct stat *s, const char **out_error_msg)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (s != NULL, FALSE);
|
2014-06-03 15:58:23 -05:00
|
|
|
g_return_val_if_fail (out_error_msg != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (*out_error_msg == NULL, FALSE);
|
2008-04-27 14:30:06 +00:00
|
|
|
|
|
|
|
|
/* Only accept files owned by root */
|
|
|
|
|
if (s->st_uid != 0) {
|
2014-06-03 15:58:23 -05:00
|
|
|
*out_error_msg = "not owned by root.";
|
2008-04-27 14:30:06 +00:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Only accept files not writable by group or other, and not SUID */
|
|
|
|
|
if (s->st_mode & (S_IWGRP | S_IWOTH | S_ISUID)) {
|
2014-06-03 15:58:23 -05:00
|
|
|
*out_error_msg = "writable by group or other, or set-UID.";
|
2008-04-27 14:30:06 +00:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Only accept files executable by the owner */
|
|
|
|
|
if (!(s->st_mode & S_IXUSR)) {
|
2014-06-03 15:58:23 -05:00
|
|
|
*out_error_msg = "not executable by owner.";
|
2008-04-27 14:30:06 +00:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-05 11:09:05 -05:00
|
|
|
static gboolean
|
|
|
|
|
check_filename (const char *file_name)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
2016-02-13 15:23:21 +01:00
|
|
|
static const char *bad_suffixes[] = {
|
|
|
|
|
"~",
|
|
|
|
|
".rpmsave",
|
|
|
|
|
".rpmorig",
|
|
|
|
|
".rpmnew",
|
|
|
|
|
".swp",
|
|
|
|
|
};
|
2008-04-27 14:30:06 +00:00
|
|
|
char *tmp;
|
2012-06-05 11:09:05 -05:00
|
|
|
guint i;
|
|
|
|
|
|
|
|
|
|
/* File must not be a backup file, package management file, or start with '.' */
|
2008-04-27 14:30:06 +00:00
|
|
|
|
|
|
|
|
if (file_name[0] == '.')
|
|
|
|
|
return FALSE;
|
2016-02-13 15:23:21 +01:00
|
|
|
for (i = 0; i < G_N_ELEMENTS (bad_suffixes); i++) {
|
2012-06-05 11:09:05 -05:00
|
|
|
if (g_str_has_suffix (file_name, bad_suffixes[i]))
|
2008-04-27 14:30:06 +00:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
2012-06-05 11:09:05 -05:00
|
|
|
tmp = g_strrstr (file_name, ".dpkg-");
|
2016-02-13 15:23:21 +01:00
|
|
|
if (tmp && !strchr (&tmp[1], '.'))
|
2008-04-27 14:30:06 +00:00
|
|
|
return FALSE;
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 11:03:25 -05:00
|
|
|
#define SCRIPT_TIMEOUT 600 /* 10 minutes */
|
|
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
static gboolean
|
|
|
|
|
script_dispatch (ScriptInfo *script)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
2012-06-05 11:09:05 -05:00
|
|
|
GError *error = NULL;
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
char *argv[4];
|
2015-07-14 18:17:33 +02:00
|
|
|
Request *request = script->request;
|
|
|
|
|
|
|
|
|
|
if (script->dispatched)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
script->dispatched = TRUE;
|
2012-06-05 11:09:05 -05:00
|
|
|
|
|
|
|
|
argv[0] = script->script;
|
2018-04-24 11:20:03 +02:00
|
|
|
argv[1] = request->iface ?: (!strcmp(request->action, NMD_ACTION_HOSTNAME) ? "none" : "");
|
2012-06-05 11:09:05 -05:00
|
|
|
argv[2] = request->action;
|
|
|
|
|
argv[3] = NULL;
|
|
|
|
|
|
2019-05-23 09:24:19 +02:00
|
|
|
_LOG_S_T (script, "run script%s", script->wait ? "" : " (no-wait)");
|
2012-06-05 11:09:05 -05:00
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
if (g_spawn_async ("/", argv, request->envp, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &script->pid, &error)) {
|
|
|
|
|
script->watch_id = g_child_watch_add (script->pid, (GChildWatchFunc) script_watch_cb, script);
|
|
|
|
|
script->timeout_id = g_timeout_add_seconds (SCRIPT_TIMEOUT, script_timeout_cb, script);
|
2015-07-27 22:44:56 +02:00
|
|
|
if (!script->wait)
|
|
|
|
|
request->num_scripts_nowait++;
|
2015-07-14 18:17:33 +02:00
|
|
|
return TRUE;
|
2012-06-05 11:09:05 -05:00
|
|
|
} else {
|
2016-02-28 18:12:28 +01:00
|
|
|
_LOG_S_W (script, "complete: failed to execute script: %s", error->message);
|
2012-06-05 11:09:05 -05:00
|
|
|
script->result = DISPATCH_RESULT_EXEC_FAILED;
|
|
|
|
|
script->error = g_strdup (error->message);
|
2015-07-14 18:17:33 +02:00
|
|
|
request->num_scripts_done++;
|
2012-06-05 11:09:05 -05:00
|
|
|
g_clear_error (&error);
|
2015-07-14 18:17:33 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-05 11:09:05 -05:00
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
static gboolean
|
|
|
|
|
dispatch_one_script (Request *request)
|
|
|
|
|
{
|
2015-07-27 22:44:56 +02:00
|
|
|
if (request->num_scripts_nowait > 0)
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
while (request->idx < request->scripts->len) {
|
|
|
|
|
ScriptInfo *script;
|
|
|
|
|
|
|
|
|
|
script = g_ptr_array_index (request->scripts, request->idx++);
|
|
|
|
|
if (script_dispatch (script))
|
|
|
|
|
return TRUE;
|
2012-06-05 11:09:05 -05:00
|
|
|
}
|
2015-07-14 18:17:33 +02:00
|
|
|
return FALSE;
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-20 16:51:46 +01:00
|
|
|
static int
|
|
|
|
|
_compare_basenames (gconstpointer a, gconstpointer b)
|
|
|
|
|
{
|
|
|
|
|
const char *basename_a = strrchr (a, '/');
|
|
|
|
|
const char *basename_b = strrchr (b, '/');
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
nm_assert (basename_a);
|
|
|
|
|
nm_assert (basename_b);
|
|
|
|
|
|
|
|
|
|
ret = strcmp (++basename_a, ++basename_b);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
nm_assert_not_reached ();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2019-05-23 08:47:07 +02:00
|
|
|
_find_scripts (Request *request, GHashTable *scripts, const char *base, const char *subdir)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
|
|
|
|
const char *filename;
|
2018-11-20 16:51:46 +01:00
|
|
|
gs_free char *dirname = NULL;
|
2008-04-27 14:30:06 +00:00
|
|
|
GError *error = NULL;
|
2018-11-20 16:51:46 +01:00
|
|
|
GDir *dir;
|
2014-06-02 14:07:46 -05:00
|
|
|
|
2018-11-20 16:51:46 +01:00
|
|
|
dirname = g_build_filename (base, "dispatcher.d", subdir, NULL);
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2014-06-02 14:07:46 -05:00
|
|
|
if (!(dir = g_dir_open (dirname, 0, &error))) {
|
2019-05-23 08:47:07 +02:00
|
|
|
if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
|
|
|
|
|
_LOG_R_W (request, "find-scripts: Failed to open dispatcher directory '%s': %s",
|
|
|
|
|
dirname, error->message);
|
|
|
|
|
}
|
2008-04-27 14:30:06 +00:00
|
|
|
g_error_free (error);
|
2018-11-20 16:51:46 +01:00
|
|
|
return;
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while ((filename = g_dir_read_name (dir))) {
|
2018-11-20 16:51:46 +01:00
|
|
|
if (!check_filename (filename))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
g_hash_table_insert (scripts,
|
|
|
|
|
g_strdup (filename),
|
|
|
|
|
g_build_filename (dirname, filename, NULL));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_dir_close (dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GSList *
|
2019-05-23 08:47:07 +02:00
|
|
|
find_scripts (Request *request)
|
2018-11-20 16:51:46 +01:00
|
|
|
{
|
|
|
|
|
gs_unref_hashtable GHashTable *scripts = NULL;
|
|
|
|
|
GSList *script_list = NULL;
|
|
|
|
|
GHashTableIter iter;
|
2019-05-23 08:47:07 +02:00
|
|
|
const char *subdir;
|
2018-11-20 16:51:46 +01:00
|
|
|
char *path;
|
|
|
|
|
char *filename;
|
|
|
|
|
|
2019-05-23 08:47:07 +02:00
|
|
|
if (NM_IN_STRSET (request->action, NMD_ACTION_PRE_UP,
|
|
|
|
|
NMD_ACTION_VPN_PRE_UP))
|
2018-11-20 16:51:46 +01:00
|
|
|
subdir = "pre-up.d";
|
2019-05-23 08:47:07 +02:00
|
|
|
else if (NM_IN_STRSET (request->action, NMD_ACTION_PRE_DOWN,
|
|
|
|
|
NMD_ACTION_VPN_PRE_DOWN))
|
2018-11-20 16:51:46 +01:00
|
|
|
subdir = "pre-down.d";
|
2019-05-23 08:47:07 +02:00
|
|
|
else
|
|
|
|
|
subdir = NULL;
|
2018-11-20 16:51:46 +01:00
|
|
|
|
|
|
|
|
scripts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
|
|
|
|
|
|
2019-05-23 08:47:07 +02:00
|
|
|
_find_scripts (request, scripts, NMLIBDIR, subdir);
|
|
|
|
|
_find_scripts (request, scripts, NMCONFDIR, subdir);
|
2018-11-20 16:51:46 +01:00
|
|
|
|
|
|
|
|
g_hash_table_iter_init (&iter, scripts);
|
|
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer *) &filename, (gpointer *) &path)) {
|
2018-02-02 10:55:34 +01:00
|
|
|
struct stat st;
|
2018-11-20 16:51:46 +01:00
|
|
|
char *link_target;
|
2008-04-27 14:30:06 +00:00
|
|
|
int err;
|
2014-06-03 15:58:23 -05:00
|
|
|
const char *err_msg = NULL;
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2018-11-20 16:51:46 +01:00
|
|
|
link_target = g_file_read_link (path, NULL);
|
|
|
|
|
if (g_strcmp0 (link_target, "/dev/null") == 0) {
|
|
|
|
|
g_free (link_target);
|
2008-04-27 14:30:06 +00:00
|
|
|
continue;
|
2018-11-20 16:51:46 +01:00
|
|
|
}
|
|
|
|
|
g_free (link_target);
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2012-06-05 11:09:05 -05:00
|
|
|
err = stat (path, &st);
|
|
|
|
|
if (err)
|
2019-05-23 08:47:07 +02:00
|
|
|
_LOG_R_W (request, "find-scripts: Failed to stat '%s': %d", path, err);
|
2018-11-20 16:51:46 +01:00
|
|
|
else if (!S_ISREG (st.st_mode))
|
2014-06-17 19:11:56 +02:00
|
|
|
; /* silently skip. */
|
2014-06-03 15:58:23 -05:00
|
|
|
else if (!check_permissions (&st, &err_msg))
|
2019-05-23 08:47:07 +02:00
|
|
|
_LOG_R_W (request, "find-scripts: Cannot execute '%s': %s", path, err_msg);
|
2014-06-03 15:58:23 -05:00
|
|
|
else {
|
2008-04-27 14:30:06 +00:00
|
|
|
/* success */
|
2018-11-20 16:51:46 +01:00
|
|
|
script_list = g_slist_prepend (script_list, g_strdup (path));
|
|
|
|
|
continue;
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 16:51:46 +01:00
|
|
|
return g_slist_sort (script_list, _compare_basenames);
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
static gboolean
|
|
|
|
|
script_must_wait (const char *path)
|
|
|
|
|
{
|
|
|
|
|
gs_free char *link = NULL;
|
|
|
|
|
gs_free char *dir = NULL;
|
|
|
|
|
gs_free char *real = NULL;
|
|
|
|
|
char *tmp;
|
|
|
|
|
|
2018-11-20 16:51:46 +01:00
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
link = g_file_read_link (path, NULL);
|
|
|
|
|
if (link) {
|
|
|
|
|
if (!g_path_is_absolute (link)) {
|
|
|
|
|
dir = g_path_get_dirname (path);
|
|
|
|
|
tmp = g_build_path ("/", dir, link, NULL);
|
|
|
|
|
g_free (link);
|
|
|
|
|
g_free (dir);
|
|
|
|
|
link = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dir = g_path_get_dirname (link);
|
|
|
|
|
real = realpath (dir, NULL);
|
2019-11-27 10:28:22 +01:00
|
|
|
if (NM_STR_HAS_SUFFIX (real, "/no-wait.d"))
|
2015-07-14 18:17:33 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 10:59:26 +02:00
|
|
|
static void
|
|
|
|
|
_method_call_action (GDBusMethodInvocation *invocation,
|
|
|
|
|
GVariant *parameters)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
2019-05-23 10:59:26 +02:00
|
|
|
const char *action;
|
|
|
|
|
gs_unref_variant GVariant *connection = NULL;
|
|
|
|
|
gs_unref_variant GVariant *connection_properties = NULL;
|
|
|
|
|
gs_unref_variant GVariant *device_properties = NULL;
|
|
|
|
|
gs_unref_variant GVariant *device_proxy_properties = NULL;
|
|
|
|
|
gs_unref_variant GVariant *device_ip4_config = NULL;
|
|
|
|
|
gs_unref_variant GVariant *device_ip6_config = NULL;
|
|
|
|
|
gs_unref_variant GVariant *device_dhcp4_config = NULL;
|
|
|
|
|
gs_unref_variant GVariant *device_dhcp6_config = NULL;
|
|
|
|
|
const char *connectivity_state;
|
|
|
|
|
const char *vpn_ip_iface;
|
|
|
|
|
gs_unref_variant GVariant *vpn_proxy_properties = NULL;
|
|
|
|
|
gs_unref_variant GVariant *vpn_ip4_config = NULL;
|
|
|
|
|
gs_unref_variant GVariant *vpn_ip6_config = NULL;
|
|
|
|
|
gboolean debug;
|
2012-10-03 17:48:40 -04:00
|
|
|
GSList *sorted_scripts = NULL;
|
|
|
|
|
GSList *iter;
|
2012-06-05 11:09:05 -05:00
|
|
|
Request *request;
|
|
|
|
|
char **p;
|
2015-07-14 18:17:33 +02:00
|
|
|
guint i, num_nowait = 0;
|
2015-08-04 10:51:32 +02:00
|
|
|
const char *error_message = NULL;
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2019-05-23 10:59:26 +02:00
|
|
|
g_variant_get (parameters, "("
|
|
|
|
|
"&s" /* action */
|
|
|
|
|
"@a{sa{sv}}" /* connection */
|
|
|
|
|
"@a{sv}" /* connection_properties */
|
|
|
|
|
"@a{sv}" /* device_properties */
|
|
|
|
|
"@a{sv}" /* device_proxy_properties */
|
|
|
|
|
"@a{sv}" /* device_ip4_config */
|
|
|
|
|
"@a{sv}" /* device_ip6_config */
|
|
|
|
|
"@a{sv}" /* device_dhcp4_config */
|
|
|
|
|
"@a{sv}" /* device_dhcp6_config */
|
|
|
|
|
"&s" /* connectivity_state */
|
|
|
|
|
"&s" /* vpn_ip_iface */
|
|
|
|
|
"@a{sv}" /* vpn_proxy_properties */
|
|
|
|
|
"@a{sv}" /* vpn_ip4_config */
|
|
|
|
|
"@a{sv}" /* vpn_ip6_config */
|
|
|
|
|
"b" /* debug */
|
|
|
|
|
")",
|
|
|
|
|
&action,
|
|
|
|
|
&connection,
|
|
|
|
|
&connection_properties,
|
|
|
|
|
&device_properties,
|
|
|
|
|
&device_proxy_properties,
|
|
|
|
|
&device_ip4_config,
|
|
|
|
|
&device_ip6_config,
|
|
|
|
|
&device_dhcp4_config,
|
|
|
|
|
&device_dhcp6_config,
|
|
|
|
|
&connectivity_state,
|
|
|
|
|
&vpn_ip_iface,
|
|
|
|
|
&vpn_proxy_properties,
|
|
|
|
|
&vpn_ip4_config,
|
|
|
|
|
&vpn_ip6_config,
|
|
|
|
|
&debug);
|
|
|
|
|
|
2015-08-03 16:15:25 +02:00
|
|
|
request = g_slice_new0 (Request);
|
2019-05-23 10:47:46 +02:00
|
|
|
request->request_id = ++gl.request_id_counter;
|
2019-05-23 10:59:26 +02:00
|
|
|
request->debug = debug || gl.debug;
|
|
|
|
|
request->context = invocation;
|
|
|
|
|
request->action = g_strdup (action);
|
|
|
|
|
|
|
|
|
|
request->envp = nm_dispatcher_utils_construct_envp (action,
|
|
|
|
|
connection,
|
|
|
|
|
connection_properties,
|
|
|
|
|
device_properties,
|
|
|
|
|
device_proxy_properties,
|
|
|
|
|
device_ip4_config,
|
|
|
|
|
device_ip6_config,
|
|
|
|
|
device_dhcp4_config,
|
|
|
|
|
device_dhcp6_config,
|
2016-07-20 16:24:41 +01:00
|
|
|
connectivity_state,
|
2012-06-05 11:09:05 -05:00
|
|
|
vpn_ip_iface,
|
2019-05-23 10:59:26 +02:00
|
|
|
vpn_proxy_properties,
|
|
|
|
|
vpn_ip4_config,
|
|
|
|
|
vpn_ip6_config,
|
2015-08-04 10:51:32 +02:00
|
|
|
&request->iface,
|
|
|
|
|
&error_message);
|
|
|
|
|
|
2013-10-18 16:07:26 +02:00
|
|
|
request->scripts = g_ptr_array_new_full (5, script_info_free);
|
2019-05-23 08:47:07 +02:00
|
|
|
|
|
|
|
|
sorted_scripts = find_scripts (request);
|
2012-10-03 17:48:40 -04:00
|
|
|
for (iter = sorted_scripts; iter; iter = g_slist_next (iter)) {
|
2015-08-03 16:15:25 +02:00
|
|
|
ScriptInfo *s;
|
|
|
|
|
|
|
|
|
|
s = g_slice_new0 (ScriptInfo);
|
2012-10-03 17:48:40 -04:00
|
|
|
s->request = request;
|
|
|
|
|
s->script = iter->data;
|
2015-07-14 18:17:33 +02:00
|
|
|
s->wait = script_must_wait (s->script);
|
2012-10-03 17:48:40 -04:00
|
|
|
g_ptr_array_add (request->scripts, s);
|
|
|
|
|
}
|
|
|
|
|
g_slist_free (sorted_scripts);
|
2015-08-03 16:17:34 +02:00
|
|
|
|
2019-05-23 09:24:19 +02:00
|
|
|
_LOG_R_D (request, "new request (%u scripts)", request->scripts->len);
|
|
|
|
|
if ( _LOG_R_T_enabled (request)
|
2016-01-19 11:01:33 +01:00
|
|
|
&& request->envp) {
|
|
|
|
|
for (p = request->envp; *p; p++)
|
2019-05-23 09:24:19 +02:00
|
|
|
_LOG_R_T (request, "environment: %s", *p);
|
2016-01-19 11:01:33 +01:00
|
|
|
}
|
2015-08-03 15:52:11 +02:00
|
|
|
|
|
|
|
|
if (error_message || request->scripts->len == 0) {
|
2015-08-04 10:51:32 +02:00
|
|
|
GVariant *results;
|
|
|
|
|
|
2015-08-03 15:52:11 +02:00
|
|
|
if (error_message)
|
|
|
|
|
_LOG_R_W (request, "completed: invalid request: %s", error_message);
|
|
|
|
|
else
|
2019-05-23 09:24:19 +02:00
|
|
|
_LOG_R_D (request, "completed: no scripts");
|
2015-08-03 15:52:11 +02:00
|
|
|
|
2015-08-04 10:51:32 +02:00
|
|
|
results = g_variant_new_array (G_VARIANT_TYPE ("(sus)"), NULL, 0);
|
2019-05-23 10:59:26 +02:00
|
|
|
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(@a(sus))", results));
|
2015-10-30 13:44:15 +01:00
|
|
|
request->num_scripts_done = request->scripts->len;
|
2015-08-04 10:51:32 +02:00
|
|
|
request_free (request);
|
2019-05-23 10:59:26 +02:00
|
|
|
return;
|
2015-08-04 10:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-23 10:47:46 +02:00
|
|
|
nm_clear_g_source (&gl.quit_id);
|
2015-08-03 16:17:34 +02:00
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
gl.num_requests_pending++;
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
for (i = 0; i < request->scripts->len; i++) {
|
|
|
|
|
ScriptInfo *s = g_ptr_array_index (request->scripts, i);
|
|
|
|
|
|
|
|
|
|
if (!s->wait) {
|
|
|
|
|
script_dispatch (s);
|
|
|
|
|
num_nowait++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (num_nowait < request->scripts->len) {
|
|
|
|
|
/* The request has at least one wait script.
|
|
|
|
|
* Try next_request() to schedule the request for
|
|
|
|
|
* execution. This either enqueues the request or
|
2019-05-23 11:45:20 +02:00
|
|
|
* sets it as gl.current_request. */
|
|
|
|
|
if (next_request (request)) {
|
2015-07-14 18:17:33 +02:00
|
|
|
/* @request is now @current_request. Go ahead and
|
|
|
|
|
* schedule the first wait script. */
|
|
|
|
|
if (!dispatch_one_script (request)) {
|
|
|
|
|
/* If that fails, we might be already finished with the
|
|
|
|
|
* request. Try complete_request(). */
|
|
|
|
|
complete_request (request);
|
|
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
if (next_request (NULL)) {
|
2015-07-14 18:17:33 +02:00
|
|
|
/* As @request was successfully scheduled as next_request(), there is no
|
|
|
|
|
* other request in queue that can be scheduled afterwards. Assert against
|
|
|
|
|
* that, but call next_request() to clear current_request. */
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* The request contains only no-wait scripts. Try to complete
|
|
|
|
|
* the request right away (we might have failed to schedule any
|
|
|
|
|
* of the scripts). It will be either completed now, or later
|
|
|
|
|
* when the pending scripts return.
|
2019-05-23 11:45:20 +02:00
|
|
|
* We don't enqueue it to gl.requests_waiting.
|
2015-07-14 18:17:33 +02:00
|
|
|
* There is no need to handle next_request(), because @request is
|
|
|
|
|
* not the current request anyway and does not interfere with requests
|
|
|
|
|
* that have any "wait" scripts. */
|
|
|
|
|
complete_request (request);
|
|
|
|
|
}
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2014-09-03 12:27:33 -04:00
|
|
|
on_name_acquired (GDBusConnection *connection,
|
|
|
|
|
const char *name,
|
|
|
|
|
gpointer user_data)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
2019-05-23 10:58:35 +02:00
|
|
|
gl.ever_acquired_name = TRUE;
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
2014-09-03 12:27:33 -04:00
|
|
|
static void
|
|
|
|
|
on_name_lost (GDBusConnection *connection,
|
|
|
|
|
const char *name,
|
|
|
|
|
gpointer user_data)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
2014-09-03 12:27:33 -04:00
|
|
|
if (!connection) {
|
2019-05-23 10:58:35 +02:00
|
|
|
if (!gl.ever_acquired_name) {
|
2019-05-23 09:46:47 +02:00
|
|
|
_LOG_X_W ("Could not get the system bus. Make sure the message bus daemon is running!");
|
2019-05-23 11:18:48 +02:00
|
|
|
gl.exit_with_failure = TRUE;
|
2015-06-30 11:55:24 +02:00
|
|
|
} else {
|
2019-05-23 09:46:47 +02:00
|
|
|
_LOG_X_I ("System bus stopped. Exiting");
|
2015-06-30 11:55:24 +02:00
|
|
|
}
|
2019-05-23 10:58:35 +02:00
|
|
|
} else if (!gl.ever_acquired_name) {
|
2019-05-23 09:46:47 +02:00
|
|
|
_LOG_X_W ("Could not acquire the " NM_DISPATCHER_DBUS_SERVICE " service.");
|
2019-05-23 11:18:48 +02:00
|
|
|
gl.exit_with_failure = TRUE;
|
|
|
|
|
} else
|
2019-05-23 09:46:47 +02:00
|
|
|
_LOG_X_I ("Lost the " NM_DISPATCHER_DBUS_SERVICE " name. Exiting");
|
2019-05-23 11:18:48 +02:00
|
|
|
|
|
|
|
|
g_main_loop_quit (gl.loop);
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-23 10:59:26 +02:00
|
|
|
static void
|
|
|
|
|
_method_call (GDBusConnection *connection,
|
|
|
|
|
const char *sender,
|
|
|
|
|
const char *object_path,
|
|
|
|
|
const char *interface_name,
|
|
|
|
|
const char *method_name,
|
|
|
|
|
GVariant *parameters,
|
|
|
|
|
GDBusMethodInvocation *invocation,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
if (nm_streq (interface_name, NM_DISPATCHER_DBUS_INTERFACE)) {
|
|
|
|
|
if (nm_streq (method_name, "Action")) {
|
|
|
|
|
_method_call_action (invocation, parameters);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g_dbus_method_invocation_return_error (invocation,
|
|
|
|
|
G_DBUS_ERROR,
|
|
|
|
|
G_DBUS_ERROR_UNKNOWN_METHOD,
|
|
|
|
|
"Unknown method %s",
|
|
|
|
|
method_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GDBusInterfaceInfo *const interface_info = NM_DEFINE_GDBUS_INTERFACE_INFO (
|
|
|
|
|
NM_DISPATCHER_DBUS_INTERFACE,
|
|
|
|
|
.methods = NM_DEFINE_GDBUS_METHOD_INFOS (
|
|
|
|
|
NM_DEFINE_GDBUS_METHOD_INFO (
|
|
|
|
|
"Action",
|
|
|
|
|
.in_args = NM_DEFINE_GDBUS_ARG_INFOS (
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("action", "s"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("connection", "a{sa{sv}}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("connection_properties", "a{sv}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("device_properties", "a{sv}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("device_proxy_properties", "a{sv}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("device_ip4_config", "a{sv}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("device_ip6_config", "a{sv}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("device_dhcp4_config", "a{sv}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("device_dhcp6_config", "a{sv}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("connectivity_state", "s"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("vpn_ip_iface", "s"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("vpn_proxy_properties", "a{sv}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("vpn_ip4_config", "a{sv}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("vpn_ip6_config", "a{sv}"),
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("debug", "b"),
|
|
|
|
|
),
|
|
|
|
|
.out_args = NM_DEFINE_GDBUS_ARG_INFOS (
|
|
|
|
|
NM_DEFINE_GDBUS_ARG_INFO ("results", "a(sus)"),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
static const GDBusInterfaceVTable interface_vtable = {
|
|
|
|
|
.method_call = _method_call,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2008-04-27 14:30:06 +00:00
|
|
|
static void
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
log_handler (const char *log_domain,
|
2008-04-27 14:30:06 +00:00
|
|
|
GLogLevelFlags log_level,
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
const char *message,
|
2008-04-27 14:30:06 +00:00
|
|
|
gpointer ignored)
|
|
|
|
|
{
|
2015-07-14 18:17:33 +02:00
|
|
|
int syslog_priority;
|
2008-04-27 14:30:06 +00:00
|
|
|
|
|
|
|
|
switch (log_level) {
|
2012-06-05 11:09:05 -05:00
|
|
|
case G_LOG_LEVEL_ERROR:
|
|
|
|
|
syslog_priority = LOG_CRIT;
|
|
|
|
|
break;
|
|
|
|
|
case G_LOG_LEVEL_CRITICAL:
|
|
|
|
|
syslog_priority = LOG_ERR;
|
|
|
|
|
break;
|
|
|
|
|
case G_LOG_LEVEL_WARNING:
|
|
|
|
|
syslog_priority = LOG_WARNING;
|
|
|
|
|
break;
|
|
|
|
|
case G_LOG_LEVEL_MESSAGE:
|
|
|
|
|
syslog_priority = LOG_NOTICE;
|
|
|
|
|
break;
|
|
|
|
|
case G_LOG_LEVEL_DEBUG:
|
|
|
|
|
syslog_priority = LOG_DEBUG;
|
|
|
|
|
break;
|
|
|
|
|
case G_LOG_LEVEL_INFO:
|
|
|
|
|
default:
|
|
|
|
|
syslog_priority = LOG_INFO;
|
|
|
|
|
break;
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
syslog (syslog_priority, "%s", message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
logging_setup (void)
|
|
|
|
|
{
|
|
|
|
|
openlog (G_LOG_DOMAIN, LOG_CONS, LOG_DAEMON);
|
2015-07-14 18:17:33 +02:00
|
|
|
g_log_set_handler (G_LOG_DOMAIN,
|
2008-04-27 14:30:06 +00:00
|
|
|
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
|
|
|
|
|
log_handler,
|
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
logging_shutdown (void)
|
|
|
|
|
{
|
|
|
|
|
closelog ();
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-18 23:37:58 +01:00
|
|
|
static gboolean
|
|
|
|
|
signal_handler (gpointer user_data)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
2013-11-18 23:37:58 +01:00
|
|
|
int signo = GPOINTER_TO_INT (user_data);
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2019-05-23 09:46:47 +02:00
|
|
|
_LOG_X_I ("Caught signal %d, shutting down...", signo);
|
2019-05-23 10:47:46 +02:00
|
|
|
g_main_loop_quit (gl.loop);
|
2013-11-18 23:37:58 +01:00
|
|
|
|
2019-05-23 10:49:06 +02:00
|
|
|
return G_SOURCE_CONTINUE;
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-23 11:24:16 +02:00
|
|
|
static gboolean
|
|
|
|
|
parse_command_line (int *p_argc,
|
|
|
|
|
char ***p_argv,
|
|
|
|
|
GError **error)
|
2008-04-27 14:30:06 +00:00
|
|
|
{
|
|
|
|
|
GOptionContext *opt_ctx;
|
|
|
|
|
GOptionEntry entries[] = {
|
2019-05-23 10:47:46 +02:00
|
|
|
{ "debug", 0, 0, G_OPTION_ARG_NONE, &gl.debug, "Output to console rather than syslog", NULL },
|
|
|
|
|
{ "persist", 0, 0, G_OPTION_ARG_NONE, &gl.persist, "Don't quit after a short timeout", NULL },
|
2008-04-27 14:30:06 +00:00
|
|
|
{ NULL }
|
|
|
|
|
};
|
2019-05-23 11:24:16 +02:00
|
|
|
gboolean success;
|
2008-04-27 14:30:06 +00:00
|
|
|
|
|
|
|
|
opt_ctx = g_option_context_new (NULL);
|
|
|
|
|
g_option_context_set_summary (opt_ctx, "Executes scripts upon actions by NetworkManager.");
|
|
|
|
|
g_option_context_add_main_entries (opt_ctx, entries, NULL);
|
|
|
|
|
|
2019-05-23 11:24:16 +02:00
|
|
|
success = g_option_context_parse (opt_ctx, p_argc, p_argv, error);
|
|
|
|
|
|
|
|
|
|
g_option_context_free (opt_ctx);
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
gs_free_error GError *error = NULL;
|
|
|
|
|
guint signal_id_term = 0;
|
|
|
|
|
guint signal_id_int = 0;
|
2019-05-23 10:59:26 +02:00
|
|
|
guint dbus_regist_id = 0;
|
2019-05-23 17:35:55 +02:00
|
|
|
guint dbus_own_name_id = 0;
|
2019-05-23 11:24:16 +02:00
|
|
|
|
|
|
|
|
if (!parse_command_line (&argc, &argv, &error)) {
|
2019-05-23 09:46:47 +02:00
|
|
|
_LOG_X_W ("Error parsing command line arguments: %s", error->message);
|
2019-05-23 11:18:48 +02:00
|
|
|
gl.exit_with_failure = TRUE;
|
|
|
|
|
goto done;
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-23 10:49:06 +02:00
|
|
|
signal_id_term = g_unix_signal_add (SIGTERM, signal_handler, GINT_TO_POINTER (SIGTERM));
|
|
|
|
|
signal_id_int = g_unix_signal_add (SIGINT, signal_handler, GINT_TO_POINTER (SIGINT));
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2019-05-23 10:47:46 +02:00
|
|
|
if (gl.debug) {
|
2016-02-24 15:17:02 +01:00
|
|
|
if (!g_getenv ("G_MESSAGES_DEBUG")) {
|
|
|
|
|
/* we log our regular messages using g_debug() and g_info().
|
|
|
|
|
* When we redirect glib logging to syslog, there is no problem.
|
|
|
|
|
* But in "debug" mode, glib will no print these messages unless
|
|
|
|
|
* we set G_MESSAGES_DEBUG. */
|
|
|
|
|
g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
|
|
|
|
|
}
|
|
|
|
|
} else
|
2008-04-27 14:30:06 +00:00
|
|
|
logging_setup ();
|
|
|
|
|
|
2019-05-23 10:47:46 +02:00
|
|
|
gl.loop = g_main_loop_new (NULL, FALSE);
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2019-05-23 11:27:33 +02:00
|
|
|
gl.dbus_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
|
|
|
|
|
if (!gl.dbus_connection) {
|
2019-05-23 09:46:47 +02:00
|
|
|
_LOG_X_W ("Could not get the system bus (%s). Make sure the message bus daemon is running!",
|
|
|
|
|
error->message);
|
2019-05-23 11:18:48 +02:00
|
|
|
gl.exit_with_failure = TRUE;
|
|
|
|
|
goto done;
|
2014-09-03 12:27:33 -04:00
|
|
|
}
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2019-05-23 11:45:20 +02:00
|
|
|
gl.requests_waiting = g_queue_new ();
|
2019-05-23 10:59:26 +02:00
|
|
|
|
|
|
|
|
dbus_regist_id = g_dbus_connection_register_object (gl.dbus_connection,
|
|
|
|
|
NM_DISPATCHER_DBUS_PATH,
|
|
|
|
|
interface_info,
|
|
|
|
|
NM_UNCONST_PTR (GDBusInterfaceVTable, &interface_vtable),
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
&error);
|
|
|
|
|
if (dbus_regist_id == 0) {
|
2019-05-23 09:46:47 +02:00
|
|
|
_LOG_X_W ("Could not export Dispatcher D-Bus interface: %s", error->message);
|
2019-05-23 11:18:48 +02:00
|
|
|
gl.exit_with_failure = 1;
|
|
|
|
|
goto done;
|
2014-09-03 12:27:33 -04:00
|
|
|
}
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2019-05-23 17:35:55 +02:00
|
|
|
dbus_own_name_id = g_bus_own_name_on_connection (gl.dbus_connection,
|
|
|
|
|
NM_DISPATCHER_DBUS_SERVICE,
|
|
|
|
|
G_BUS_NAME_OWNER_FLAGS_NONE,
|
|
|
|
|
on_name_acquired,
|
|
|
|
|
on_name_lost,
|
|
|
|
|
NULL, NULL);
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2015-07-14 18:17:33 +02:00
|
|
|
quit_timeout_reschedule ();
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2019-05-23 10:47:46 +02:00
|
|
|
g_main_loop_run (gl.loop);
|
2008-04-27 14:30:06 +00:00
|
|
|
|
2019-05-23 11:18:48 +02:00
|
|
|
done:
|
|
|
|
|
|
2019-05-24 09:14:45 +02:00
|
|
|
if (gl.num_requests_pending > 0) {
|
|
|
|
|
/* this only happens when we quit due to SIGTERM (not due to the idle timer).
|
|
|
|
|
*
|
|
|
|
|
* Log a warning about pending scripts.
|
|
|
|
|
*
|
|
|
|
|
* Maybe we should notify NetworkManager that these scripts are left in an unknown state.
|
|
|
|
|
* But this is either a bug of a dispatcher script (not terminating in time).
|
|
|
|
|
*
|
|
|
|
|
* FIXME(shutdown): Also, currently NetworkManager behaves wrongly on shutdown.
|
|
|
|
|
* Note that systemd would not terminate NetworkManager-dispatcher before NetworkManager.
|
|
|
|
|
* It's NetworkManager's responsibility to keep running long enough so that all requests
|
|
|
|
|
* can complete (with a watchdog timer, and a warning that user provided scripts hang). */
|
|
|
|
|
_LOG_X_W ("exiting but there are still %u requests pending", gl.num_requests_pending);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 17:35:55 +02:00
|
|
|
if (dbus_own_name_id != 0)
|
|
|
|
|
g_bus_unown_name (nm_steal_int (&dbus_own_name_id));
|
|
|
|
|
|
2019-05-23 10:59:26 +02:00
|
|
|
if (dbus_regist_id != 0)
|
|
|
|
|
g_dbus_connection_unregister_object (gl.dbus_connection, nm_steal_int (&dbus_regist_id));
|
2019-05-23 11:18:48 +02:00
|
|
|
|
2019-05-23 10:59:26 +02:00
|
|
|
nm_clear_pointer (&gl.requests_waiting, g_queue_free);
|
2014-03-31 09:04:46 -04:00
|
|
|
|
2019-05-23 10:49:06 +02:00
|
|
|
nm_clear_g_source (&signal_id_term);
|
|
|
|
|
nm_clear_g_source (&signal_id_int);
|
|
|
|
|
nm_clear_g_source (&gl.quit_id);
|
|
|
|
|
g_clear_pointer (&gl.loop, g_main_loop_unref);
|
2019-05-23 11:27:33 +02:00
|
|
|
g_clear_object (&gl.dbus_connection);
|
2019-05-23 10:49:06 +02:00
|
|
|
|
2019-05-23 10:47:46 +02:00
|
|
|
if (!gl.debug)
|
2008-04-27 14:30:06 +00:00
|
|
|
logging_shutdown ();
|
|
|
|
|
|
2019-05-23 11:18:48 +02:00
|
|
|
return gl.exit_with_failure ? 1 : 0;
|
2008-04-27 14:30:06 +00:00
|
|
|
}
|