Commit 94ed3ccc authored by David Zeuthen's avatar David Zeuthen

add spawn function to libkit

parent bcff1bde
......@@ -84,6 +84,7 @@
<xi:include href="xml/kit-list.xml"/>
<xi:include href="xml/kit-hash.xml"/>
<xi:include href="xml/kit-file.xml"/>
<xi:include href="xml/kit-spawn.xml"/>
</reference>
<reference id="ref-core">
......
......@@ -25,6 +25,7 @@ libkit_la_SOURCES = \
kit-list.h kit-list.c \
kit-hash.h kit-hash.c \
kit-file.h kit-file.c \
kit-spawn.h kit-spawn.c \
kit-message.h kit-message.c \
$(NULL)
......
......@@ -330,6 +330,42 @@ kit_strv_length (char **str_array)
return n;
}
/**
* kit_str_append:
* @s: either %NULL or a string previously allocated on the heap
* @s2: string to append
*
* Append a string to an existing string.
*
* Returns: %NULL on OOM or the new string; possibly at the same
* location as @s.
*/
char *
kit_str_append (char *s, const char *s2)
{
char *p;
size_t s_len;
size_t s2_len;
kit_return_val_if_fail (s2 != NULL, NULL);
if (s != NULL)
s_len = strlen (s);
else
s_len = 0;
s2_len = strlen (s2);
p = (char *) kit_realloc ((void *) s, s_len + s2_len + 1);
if (p == NULL)
goto oom;
s = p;
memcpy ((void *) (s + s_len), s2, s2_len);
s[s_len + s2_len] = '\0';
return s;
oom:
return NULL;
}
#ifdef KIT_BUILD_TESTS
......@@ -338,6 +374,7 @@ _run_test (void)
{
char str[] = "Hello world";
char *p;
char *p2;
char **tokens;
size_t num_tokens;
......@@ -392,6 +429,22 @@ _run_test (void)
kit_strfreev (tokens);
}
if ((p = kit_strdup ("foobar")) != NULL) {
if ((p2 = kit_str_append (p, "_cool")) != NULL) {
p = p2;
kit_assert (strcmp (p, "foobar_cool") == 0);
}
kit_free (p);
}
if ((p = kit_str_append (NULL, "baz")) != NULL) {
kit_assert (strcmp (p, "baz") == 0);
kit_free (p);
}
return TRUE;
}
......
......@@ -38,6 +38,7 @@ char *kit_strdup (const char *s);
char *kit_strndup (const char *s, size_t n);
char *kit_strdup_printf (const char *format, ...) __attribute__((__format__ (__printf__, 1, 2)));
char *kit_strdup_vprintf (const char *format, va_list args);
char *kit_str_append (char *s, const char *s2);
kit_bool_t kit_str_has_prefix (const char *s, const char *prefix);
kit_bool_t kit_str_has_suffix (const char *s, const char *suffix);
......
......@@ -38,12 +38,13 @@
*/
static KitTest *tests[] = {
&_test_message,
&_test_memory,
&_test_string,
&_test_list,
&_test_hash,
&_test_file,
&_test_message,
&_test_spawn,
};
int
......
......@@ -52,6 +52,7 @@ extern KitTest _test_string;
extern KitTest _test_hash;
extern KitTest _test_list;
extern KitTest _test_file;
extern KitTest _test_spawn;
extern KitTest _test_message;
KIT_END_DECLS
......
......@@ -132,6 +132,7 @@ do {
#include <kit/kit-list.h>
#include <kit/kit-hash.h>
#include <kit/kit-file.h>
#include <kit/kit-spawn.h>
#include <kit/kit-message.h>
#undef _KIT_INSIDE_KIT_H
......
......@@ -75,11 +75,11 @@ struct _PolKitAuthorizationDB;
/* PolKitAuthorizationDB structure is defined in polkit/polkit-private.h */
static void
_free_authlist (GSList *authlist)
_free_authlist (KitList *authlist)
{
if (authlist != NULL) {
g_slist_foreach (authlist, (GFunc) polkit_authorization_unref, NULL);
g_slist_free (authlist);
kit_list_foreach (authlist, (KitListForeachFunc) polkit_authorization_unref, NULL);
kit_list_free (authlist);
}
}
......@@ -113,11 +113,14 @@ _polkit_authorization_db_new (void)
{
PolKitAuthorizationDB *authdb;
authdb = g_new0 (PolKitAuthorizationDB, 1);
authdb = kit_new0 (PolKitAuthorizationDB, 1);
if (authdb == NULL)
goto oom;
authdb->refcount = 1;
/* set up the hashtable */
_polkit_authorization_db_invalidate_cache (authdb);
oom:
return authdb;
}
......@@ -173,7 +176,7 @@ polkit_authorization_db_unref (PolKitAuthorizationDB *authdb)
if (authdb->refcount > 0)
return;
kit_hash_unref (authdb->uid_to_authlist);
g_free (authdb);
kit_free (authdb);
}
/**
......@@ -271,7 +274,7 @@ _authdb_get_auths_for_uid (PolKitAuthorizationDB *authdb,
if (ret != NULL)
goto out;
helper_argv[1] = g_strdup_printf ("%d", uid);
helper_argv[1] = kit_strdup_printf ("%d", uid);
/* we need to do this through a setgid polkituser helper
* because the auth file is readable only for uid 0 and gid
......@@ -352,8 +355,8 @@ _authdb_get_auths_for_uid (PolKitAuthorizationDB *authdb,
kit_hash_insert (authdb->uid_to_authlist, (void *) uid, ret);
out:
g_free (helper_argv[1]);
g_free (standard_output);
kit_free (helper_argv[1]);
kit_free (standard_output);
return ret;
}
......@@ -811,7 +814,7 @@ polkit_authorization_db_revoke_entry (PolKitAuthorizationDB *authdb,
helper_argv[1] = (char *) auth_file_entry;
helper_argv[2] = "uid";
helper_argv[3] = g_strdup_printf ("%d", polkit_authorization_get_uid (auth));
helper_argv[3] = kit_strdup_printf ("%d", polkit_authorization_get_uid (auth));
g_error = NULL;
if (!g_spawn_sync (NULL, /* const gchar *working_directory */
......@@ -848,7 +851,7 @@ polkit_authorization_db_revoke_entry (PolKitAuthorizationDB *authdb,
}
out:
g_free (helper_argv[3]);
kit_free (helper_argv[3]);
return ret;
}
......
......@@ -37,8 +37,6 @@
#include <unistd.h>
#include <errno.h>
#include <glib.h>
#include "polkit-debug.h"
#include "polkit-authorization.h"
#include "polkit-utils.h"
......@@ -101,7 +99,7 @@ PolKitAuthorization *
_polkit_authorization_new_for_uid (const char *entry_in_auth_file, uid_t uid)
{
char **t;
guint num_t;
size_t num_t;
char *ep;
PolKitAuthorization *auth;
int n;
......@@ -121,8 +119,9 @@ _polkit_authorization_new_for_uid (const char *entry_in_auth_file, uid_t uid)
auth->uid = uid;
t = g_strsplit (entry_in_auth_file, ":", 0);
num_t = g_strv_length (t);
t = kit_strsplit (entry_in_auth_file, ':', &num_t);
if (t == NULL)
goto oom;
/*
* pid:
......@@ -281,7 +280,7 @@ _polkit_authorization_new_for_uid (const char *entry_in_auth_file, uid_t uid)
goto error;
}
g_strfreev (t);
kit_strfreev (t);
return auth;
error:
......@@ -290,7 +289,7 @@ oom:
if (auth != NULL)
polkit_authorization_unref (auth);
if (t != NULL)
g_strfreev (t);
kit_strfreev (t);
return NULL;
}
......
......@@ -42,7 +42,6 @@
#include <expat.h>
#include <glib.h>
#include "polkit-config.h"
#include "polkit-debug.h"
#include "polkit-error.h"
......@@ -148,7 +147,7 @@ struct ConfigNode
} data;
GSList *children;
KitList *children;
};
......@@ -156,14 +155,14 @@ static ConfigNode *
config_node_new (void)
{
ConfigNode *node;
node = g_new0 (ConfigNode, 1);
node = kit_new0 (ConfigNode, 1);
return node;
}
static void
config_node_dump_real (ConfigNode *node, unsigned int indent)
{
GSList *i;
KitList *i;
unsigned int n;
char buf[128];
......@@ -201,7 +200,7 @@ config_node_dump_real (ConfigNode *node, unsigned int indent)
break;
}
for (i = node->children; i != NULL; i = g_slist_next (i)) {
for (i = node->children; i != NULL; i = i->next) {
ConfigNode *child = i->data;
config_node_dump_real (child, indent + 2);
}
......@@ -217,7 +216,7 @@ config_node_dump (ConfigNode *node)
static void
config_node_unref (ConfigNode *node)
{
GSList *i;
KitList *i;
switch (node->node_type) {
case NODE_TYPE_NOP:
......@@ -225,22 +224,22 @@ config_node_unref (ConfigNode *node)
case NODE_TYPE_TOP:
break;
case NODE_TYPE_MATCH:
g_free (node->data.node_match.data);
kit_free (node->data.node_match.data);
regfree (&(node->data.node_match.preq));
break;
case NODE_TYPE_RETURN:
break;
case NODE_TYPE_DEFINE_ADMIN_AUTH:
g_free (node->data.node_define_admin_auth.data);
kit_free (node->data.node_define_admin_auth.data);
break;
}
for (i = node->children; i != NULL; i = g_slist_next (i)) {
for (i = node->children; i != NULL; i = i->next) {
ConfigNode *child = i->data;
config_node_unref (child);
}
g_slist_free (node->children);
g_free (node);
kit_list_free (node->children);
kit_free (node);
}
static void
......@@ -289,7 +288,7 @@ _start (void *data, const char *el, const char **attr)
goto error;
}
node->data.node_match.data = g_strdup (attr[1]);
node->data.node_match.data = kit_strdup (attr[1]);
if (regcomp (&(node->data.node_match.preq), node->data.node_match.data, REG_NOSUB|REG_EXTENDED) != 0) {
_pk_debug ("Invalid expression '%s'", node->data.node_match.data);
goto error;
......@@ -333,7 +332,7 @@ _start (void *data, const char *el, const char **attr)
goto error;
}
node->data.node_define_admin_auth.data = g_strdup (attr[1]);
node->data.node_define_admin_auth.data = kit_strdup (attr[1]);
state = STATE_IN_DEFINE_ADMIN_AUTH;
_pk_debug ("parsed define_admin_auth node ('%s' (%d) -> '%s')",
......@@ -347,8 +346,8 @@ _start (void *data, const char *el, const char **attr)
}
if (state == STATE_NONE || node == NULL) {
g_warning ("skipping unknown tag <%s> at line %d of %s",
el, (int) XML_GetCurrentLineNumber (pd->parser), pd->path);
kit_warning ("skipping unknown tag <%s> at line %d of %s",
el, (int) XML_GetCurrentLineNumber (pd->parser), pd->path);
syslog (LOG_ALERT, "libpolkit: skipping unknown tag <%s> at line %d of %s",
el, (int) XML_GetCurrentLineNumber (pd->parser), pd->path);
state = STATE_UNKNOWN_TAG;
......@@ -364,7 +363,7 @@ _start (void *data, const char *el, const char **attr)
if (pd->stack_depth > 0) {
pd->node_stack[pd->stack_depth - 1]->children =
g_slist_append (pd->node_stack[pd->stack_depth - 1]->children, node);
kit_list_append (pd->node_stack[pd->stack_depth - 1]->children, node);
}
pd->stack_depth++;
......@@ -421,19 +420,15 @@ polkit_config_new (const char *path, PolKitError **error)
int xml_res;
PolKitConfig *pk_config;
char *buf;
gsize buflen;
GError *g_error;
size_t buflen;
/* load and parse the configuration file */
pk_config = NULL;
g_error = NULL;
if (!g_file_get_contents (path, &buf, &buflen, &g_error)) {
if (!kit_file_get_contents (path, &buf, &buflen)) {
polkit_error_set_error (error, POLKIT_ERROR_POLICY_FILE_INVALID,
"Cannot load PolicyKit policy file at '%s': %s",
path,
g_error->message);
g_error_free (g_error);
"Cannot load PolicyKit policy file at '%s': %m",
path);
goto error;
}
......@@ -449,7 +444,7 @@ polkit_config_new (const char *path, PolKitError **error)
XML_SetElementHandler (pd.parser, _start, _end);
XML_SetCharacterDataHandler (pd.parser, _cdata);
pk_config = g_new0 (PolKitConfig, 1);
pk_config = kit_new0 (PolKitConfig, 1);
pk_config->refcount = 1;
pd.state = STATE_NONE;
......@@ -468,11 +463,11 @@ polkit_config_new (const char *path, PolKitError **error)
XML_ErrorString (XML_GetErrorCode (pd.parser)));
XML_ParserFree (pd.parser);
g_free (buf);
kit_free (buf);
goto error;
}
XML_ParserFree (pd.parser);
g_free (buf);
kit_free (buf);
_pk_debug ("Loaded configuration file %s", path);
......@@ -522,10 +517,10 @@ polkit_config_unref (PolKitConfig *pk_config)
if (pk_config->top_config_node != NULL)
config_node_unref (pk_config->top_config_node);
g_free (pk_config);
kit_free (pk_config);
}
static gboolean
static polkit_bool_t
config_node_match (ConfigNode *node,
PolKitAction *action,
PolKitCaller *caller,
......@@ -535,7 +530,7 @@ config_node_match (ConfigNode *node,
char *str1;
char *str2;
uid_t uid;
gboolean match;
polkit_bool_t match;
match = FALSE;
str1 = NULL;
......@@ -545,7 +540,7 @@ config_node_match (ConfigNode *node,
case MATCH_TYPE_ACTION:
if (!polkit_action_get_action_id (action, &str))
goto out;
str1 = g_strdup (str);
str1 = kit_strdup (str);
break;
case MATCH_TYPE_USER:
......@@ -558,7 +553,7 @@ config_node_match (ConfigNode *node,
} else
goto out;
str1 = g_strdup_printf ("%d", uid);
str1 = kit_strdup_printf ("%d", uid);
{
struct passwd pd;
struct passwd* pwdptr=&pd;
......@@ -568,7 +563,7 @@ config_node_match (ConfigNode *node,
if ((getpwuid_r (uid, pwdptr, pwdbuffer, pwdlinelen, &tempPwdPtr)) !=0 )
goto out;
str2 = g_strdup (pd.pw_name);
str2 = kit_strdup (pd.pw_name);
}
break;
}
......@@ -583,8 +578,8 @@ config_node_match (ConfigNode *node,
}
out:
g_free (str1);
g_free (str2);
kit_free (str1);
kit_free (str2);
return match;
}
......@@ -596,7 +591,7 @@ config_node_test (ConfigNode *node,
PolKitCaller *caller,
PolKitSession *session)
{
gboolean recurse;
polkit_bool_t recurse;
PolKitResult result;
recurse = FALSE;
......@@ -621,8 +616,8 @@ config_node_test (ConfigNode *node,
}
if (recurse) {
GSList *i;
for (i = node->children; i != NULL; i = g_slist_next (i)) {
KitList *i;
for (i = node->children; i != NULL; i = i->next) {
ConfigNode *child_node = i->data;
result = config_node_test (child_node, action, caller, session);
if (result != POLKIT_RESULT_UNKNOWN) {
......@@ -693,8 +688,8 @@ config_node_determine_admin_auth (ConfigNode *node,
PolKitConfigAdminAuthType *out_admin_auth_type,
const char **out_data)
{
gboolean recurse;
gboolean result_set;
polkit_bool_t recurse;
polkit_bool_t result_set;
recurse = FALSE;
result_set = FALSE;
......@@ -722,8 +717,8 @@ config_node_determine_admin_auth (ConfigNode *node,
}
if (recurse) {
GSList *i;
for (i = node->children; i != NULL; i = g_slist_next (i)) {
KitList *i;
for (i = node->children; i != NULL; i = i->next) {
ConfigNode *child_node = i->data;
result_set = config_node_determine_admin_auth (child_node,
......
......@@ -38,7 +38,6 @@
#include <sys/inotify.h>
#include <syslog.h>
#include <glib.h>
#include "polkit-config.h"
#include "polkit-debug.h"
#include "polkit-context.h"
......@@ -121,7 +120,7 @@ PolKitContext *
polkit_context_new (void)
{
PolKitContext *pk_context;
pk_context = g_new0 (PolKitContext, 1);
pk_context = kit_new0 (PolKitContext, 1);
pk_context->refcount = 1;
/* TODO: May want to rethink instantiating this on demand.. */
pk_context->authdb = _polkit_authorization_db_new ();
......@@ -143,7 +142,7 @@ polkit_context_init (PolKitContext *pk_context, PolKitError **error)
{
kit_return_val_if_fail (pk_context != NULL, FALSE);
pk_context->policy_dir = g_strdup (PACKAGE_DATA_DIR "/PolicyKit/policy");
pk_context->policy_dir = kit_strdup (PACKAGE_DATA_DIR "/PolicyKit/policy");
_pk_debug ("Using policy files from directory %s", pk_context->policy_dir);
/* NOTE: we don't populate the cache until it's needed.. */
......@@ -239,7 +238,7 @@ polkit_context_unref (PolKitContext *pk_context)
if (pk_context->refcount > 0)
return;
g_free (pk_context);
kit_free (pk_context);
}
/**
......@@ -285,7 +284,7 @@ polkit_context_set_config_changed (PolKitContext *pk_context,
void
polkit_context_io_func (PolKitContext *pk_context, int fd)
{
gboolean config_changed;
polkit_bool_t config_changed;
kit_return_if_fail (pk_context != NULL);
......@@ -410,7 +409,7 @@ polkit_context_get_policy_cache (PolKitContext *pk_context)
pk_context->load_descriptions,
&error);
if (pk_context->priv_cache == NULL) {
g_warning ("Error loading policy files from %s: %s",
kit_warning ("Error loading policy files from %s: %s",
pk_context->policy_dir, polkit_error_get_error_message (error));
polkit_error_free (error);
} else {
......@@ -481,9 +480,9 @@ polkit_context_is_session_authorized (PolKitContext *pk_context,
if (pfe == NULL) {
char *action_name;
if (!polkit_action_get_action_id (action, &action_name)) {
g_warning ("given action has no name");
kit_warning ("given action has no name");
} else {
g_warning ("no action with name '%s'", action_name);
kit_warning ("no action with name '%s'", action_name);
}
result = POLKIT_RESULT_UNKNOWN;
goto out;
......@@ -533,7 +532,7 @@ polkit_context_is_session_authorized (PolKitContext *pk_context,
/* Otherwise, fall back to defaults as specified in the .policy file */
policy_default = polkit_policy_file_entry_get_default (pfe);
if (policy_default == NULL) {
g_warning ("no default policy for action!");
kit_warning ("no default policy for action!");
goto out;
}
result = polkit_policy_default_can_session_do_action (policy_default, action, session);
......@@ -631,9 +630,9 @@ polkit_context_is_caller_authorized (PolKitContext *pk_context,
if (pfe == NULL) {
char *action_name;
if (!polkit_action_get_action_id (action, &action_name)) {
g_warning ("given action has no name");
kit_warning ("given action has no name");
} else {
g_warning ("no action with name '%s'", action_name);
kit_warning ("no action with name '%s'", action_name);
}
result = POLKIT_RESULT_UNKNOWN;
goto out;
......@@ -684,7 +683,7 @@ polkit_context_is_caller_authorized (PolKitContext *pk_context,
/* Otherwise, fall back to defaults as specified in the .policy file */
policy_default = polkit_policy_file_entry_get_default (pfe);
if (policy_default == NULL) {
g_warning ("no default policy for action!");
kit_warning ("no default policy for action!");
goto out;
}
result = polkit_policy_default_can_caller_do_action (policy_default, action, caller);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment