static int bus_get_selinux_security_context( DBusConnection *connection, const char *name, char **scon, DBusError *error) { _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL; DBusMessageIter iter, sub; const char *bytes; char *b; int nbytes; m = dbus_message_new_method_call( DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetConnectionSELinuxSecurityContext"); if (!m) { dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL); return -ENOMEM; } if (!dbus_message_append_args( m, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) { dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL); return -ENOMEM; } reply = dbus_connection_send_with_reply_and_block(connection, m, -1, error); if (!reply) return -EIO; if (dbus_set_error_from_message(error, reply)) return -EIO; if (!dbus_message_iter_init(reply, &iter)) return -EIO; if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) return -EIO; dbus_message_iter_recurse(&iter, &sub); dbus_message_iter_get_fixed_array(&sub, &bytes, &nbytes); b = strndup(bytes, nbytes); if (!b) return -ENOMEM; *scon = b; log_debug("GetConnectionSELinuxSecurityContext %s (pid %ld)", *scon, (long) bus_get_unix_process_id(connection, name, error)); return 0; }
int sbus_introspect(struct sbus_request *dbus_req, void *pvt) { char *xml; DBusError dberr; const struct sbus_interface_meta *iface; struct sbus_introspect_ctx *ictx; ictx = talloc_get_type(pvt, struct sbus_introspect_ctx); if (ictx == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "Invalid pointer!\n"); return sbus_request_return_and_finish(dbus_req, DBUS_TYPE_INVALID); } iface = ictx->iface; xml = sbus_introspect_xml(dbus_req, iface); if (xml == NULL) { dbus_error_init(&dberr); dbus_set_error_const(&dberr, DBUS_ERROR_NO_MEMORY, "Failed to generate introspection data\n"); return sbus_request_fail_and_finish(dbus_req, &dberr); } return sbus_request_return_and_finish(dbus_req, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID); }
void sbus_request_invoke_or_finish(struct sbus_request *dbus_req, sbus_msg_handler_fn handler_fn, void *handler_data, sbus_method_invoker_fn invoker_fn) { DBusError error; int ret; if (invoker_fn != NULL) { ret = invoker_fn(dbus_req, handler_fn); } else if (handler_fn != NULL) { ret = handler_fn(dbus_req, handler_data); } else { ret = EINVAL; } switch(ret) { case ERR_SBUS_REQUEST_HANDLED: case EOK: return; case ENOMEM: DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory handling DBus message\n"); sbus_request_finish(dbus_req, NULL); break; default: DEBUG(SSSDBG_CRIT_FAILURE, "Handler failed [%d]: %s\n", ret, sss_strerror(ret)); dbus_error_init(&error); dbus_set_error_const(&error, DBUS_ERROR_FAILED, INTERNAL_ERROR); sbus_request_fail_and_finish(dbus_req, &error); break; } }
static DBusError *sbus_error_new_va(TALLOC_CTX *mem_ctx, const char *error_name, const char *fmt, va_list ap) { DBusError *error; const char *error_msg; error = talloc_zero(mem_ctx, DBusError); if (error == NULL) { return NULL; } if (fmt != NULL) { error_msg = talloc_vasprintf(error, fmt, ap); if (error_msg == NULL) { talloc_free(error); return NULL; } } else { error_msg = NULL; } dbus_error_init(error); dbus_set_error_const(error, error_name, error_msg); return error; }
static void pincode_reply(DBusPendingCall *call, void *user_data) { struct agent_request *req = user_data; struct agent *agent = req->agent; struct btd_adapter *adapter = agent->adapter; agent_pincode_cb cb = req->cb; DBusMessage *message; DBusError err; bdaddr_t sba; size_t len; char *pin; adapter_get_address(adapter, &sba); /* steal_reply will always return non-NULL since the callback * is only called after a reply has been received */ message = dbus_pending_call_steal_reply(call); dbus_error_init(&err); if (dbus_set_error_from_message(&err, message)) { error("Agent %s replied with an error: %s, %s", agent->path, err.name, err.message); cb(agent, &err, NULL, req->user_data); dbus_error_free(&err); goto done; } dbus_error_init(&err); if (!dbus_message_get_args(message, &err, DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID)) { error("Wrong passkey reply signature: %s", err.message); cb(agent, &err, NULL, req->user_data); dbus_error_free(&err); goto done; } len = strlen(pin); dbus_error_init(&err); if (len > 16 || len < 1) { error("Invalid PIN length (%zu) from agent", len); dbus_set_error_const(&err, "org.bluez.Error.InvalidArgs", "Invalid passkey length"); cb(agent, &err, NULL, req->user_data); dbus_error_free(&err); goto done; } cb(agent, NULL, pin, req->user_data); done: if (message) dbus_message_unref(message); dbus_pending_call_cancel(req->call); agent->request = NULL; agent_request_free(req, TRUE); }
static DBusHandlerResult session_message_handler( DBusConnection *connection, DBusMessage *message, void *userdata) { Manager *m = userdata; Session *s; int r; r = get_session_for_path(m, dbus_message_get_path(message), &s); if (r < 0) { if (r == -ENOMEM) return DBUS_HANDLER_RESULT_NEED_MEMORY; if (r == -ENOENT) { DBusError e; dbus_error_init(&e); dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session"); return bus_send_error_reply(connection, message, &e, r); } return bus_send_error_reply(connection, message, NULL, r); } return session_message_dispatch(s, connection, message); }
static dbus_bool_t fill_dict_with_properties( DBusMessageIter *dict_iter, const struct wpa_dbus_property_desc *props, const char *interface, void *user_data, DBusError *error) { DBusMessageIter entry_iter; const struct wpa_dbus_property_desc *dsc; for (dsc = props; dsc && dsc->dbus_property; dsc++) { /* Only return properties for the requested D-Bus interface */ if (os_strncmp(dsc->dbus_interface, interface, WPAS_DBUS_INTERFACE_MAX) != 0) continue; /* Skip write-only properties */ if (dsc->getter == NULL) continue; if (!dbus_message_iter_open_container(dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &entry_iter)) { dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory"); return FALSE; } if (!dbus_message_iter_append_basic(&entry_iter, DBUS_TYPE_STRING, &dsc->dbus_property)) { dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory"); return FALSE; } /* An error getting a property fails the request entirely */ if (!dsc->getter(&entry_iter, error, user_data)) return FALSE; dbus_message_iter_close_container(dict_iter, &entry_iter); } return TRUE; }
/** * Start reading from a dbus dict. * * @param iter A valid DBusMessageIter pointing to the start of the dict * @param iter_dict (out) A DBusMessageIter to be passed to * wpa_dbus_dict_read_next_entry() * @error on failure a descriptive error * @return TRUE on success, FALSE on failure * */ dbus_bool_t wpa_dbus_dict_open_read(DBusMessageIter *iter, DBusMessageIter *iter_dict, DBusError *error) { if (!iter || !iter_dict) { dbus_set_error_const(error, DBUS_ERROR_FAILED, "[internal] missing message iterators"); return FALSE; } if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY || dbus_message_iter_get_element_type(iter) != DBUS_TYPE_DICT_ENTRY) { dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS, "unexpected message argument types"); return FALSE; } dbus_message_iter_recurse(iter, iter_dict); return TRUE; }
static int sync_auth(DBusConnection *bus, DBusError *error) { usec_t begin, tstamp; assert(bus); /* This complexity should probably move into D-Bus itself: * * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */ begin = tstamp = now(CLOCK_MONOTONIC); for (;;) { if (tstamp > begin + DEFAULT_TIMEOUT_USEC) break; if (dbus_connection_get_is_authenticated(bus)) break; if (!dbus_connection_read_write_dispatch(bus, ((begin + DEFAULT_TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC)) break; tstamp = now(CLOCK_MONOTONIC); } if (!dbus_connection_get_is_connected(bus)) { dbus_set_error_const(error, DBUS_ERROR_NO_SERVER, "Connection terminated during authentication."); return -ECONNREFUSED; } if (!dbus_connection_get_is_authenticated(bus)) { dbus_set_error_const(error, DBUS_ERROR_TIMEOUT, "Failed to authenticate in time."); return -EACCES; } return 0; }
void agent_unref(struct agent *agent) { agent->ref--; DBG("%p: ref=%d", agent, agent->ref); if (agent->ref > 0) return; if (agent->request) { DBusError err; agent_pincode_cb pincode_cb; agent_passkey_cb passkey_cb; agent_cb cb; dbus_error_init(&err); dbus_set_error_const(&err, ERROR_INTERFACE ".Failed", "Canceled"); switch (agent->request->type) { case AGENT_REQUEST_PINCODE: pincode_cb = agent->request->cb; pincode_cb(agent, &err, NULL, agent->request->user_data); break; case AGENT_REQUEST_PASSKEY: passkey_cb = agent->request->cb; passkey_cb(agent, &err, 0, agent->request->user_data); break; case AGENT_REQUEST_CONFIRMATION: case AGENT_REQUEST_AUTHORIZATION: case AGENT_REQUEST_AUTHORIZE_SERVICE: case AGENT_REQUEST_DISPLAY_PINCODE: default: cb = agent->request->cb; cb(agent, &err, agent->request->user_data); } dbus_error_free(&err); agent_cancel(agent); } g_free(agent->owner); g_free(agent->path); g_free(agent); }
void agent_free(struct agent *agent) { if (!agent) return; if (agent->remove_cb) agent->remove_cb(agent, agent->remove_cb_data); if (agent->request) { DBusError err; agent_pincode_cb pincode_cb; agent_passkey_cb passkey_cb; agent_cb cb; dbus_error_init(&err); dbus_set_error_const(&err, "org.bluez.Error.Failed", "Canceled"); switch (agent->request->type) { case AGENT_REQUEST_PINCODE: pincode_cb = agent->request->cb; pincode_cb(agent, &err, NULL, agent->request->user_data); break; case AGENT_REQUEST_PASSKEY: passkey_cb = agent->request->cb; passkey_cb(agent, &err, 0, agent->request->user_data); break; default: cb = agent->request->cb; cb(agent, &err, agent->request->user_data); } dbus_error_free(&err); agent_cancel(agent); } if (!agent->exited) { g_dbus_remove_watch(btd_get_dbus_connection(), agent->listener_id); agent_release(agent); } g_free(agent->name); g_free(agent->path); g_free(agent); }
/** * @ingroup DBusGLibInternals * Unit test for general glib stuff * @returns #TRUE on success. */ gboolean _dbus_glib_test (const char *test_data_dir) { DBusError err; GError *gerror = NULL; dbus_error_init (&err); dbus_set_error_const (&err, DBUS_ERROR_NO_MEMORY, "Out of memory!"); dbus_set_g_error (&gerror, &err); g_assert (gerror != NULL); g_assert (gerror->domain == DBUS_GERROR); g_assert (gerror->code == DBUS_GERROR_NO_MEMORY); g_assert (!strcmp (gerror->message, "Out of memory!")); dbus_error_init (&err); g_clear_error (&gerror); return TRUE; }
int sbus_request_return_and_finish(struct sbus_request *dbus_req, int first_arg_type, ...) { DBusMessage *reply; DBusError error = DBUS_ERROR_INIT; dbus_bool_t dbret; va_list va; int ret; va_start(va, first_arg_type); ret = sbus_request_valist_check(va, first_arg_type); if (ret != EOK) { va_end(va); dbus_set_error_const(&error, DBUS_ERROR_INVALID_ARGS, INTERNAL_ERROR); return sbus_request_fail_and_finish(dbus_req, &error); } reply = dbus_message_new_method_return(dbus_req->message); if (!reply) { va_end(va); DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory allocating DBus message\n"); sbus_request_finish(dbus_req, NULL); return ENOMEM; } dbret = dbus_message_append_args_valist(reply, first_arg_type, va); va_end(va); if (dbret) { ret = sbus_request_finish(dbus_req, reply); } else { DEBUG(SSSDBG_CRIT_FAILURE, "Couldn't build DBus message\n"); sbus_request_finish(dbus_req, NULL); ret = EINVAL; } dbus_message_unref(reply); return ret; }
bool dbus_proc_property(const char *method, DBusMessage *msg, DBusMessage *reply, DBusError *error, struct gsh_dbus_interface **interfaces) { const char *interface; const char *prop_name; bool retval = false; struct gsh_dbus_interface **iface; struct gsh_dbus_prop **prop; DBusMessageIter reply_iter; dbus_message_iter_init_append(reply, &reply_iter); if (strcmp(method, "GetAll") == 0) { DBusMessageIter getall_dict, dict_entry, val_iter; if (!dbus_message_get_args (msg, error, DBUS_TYPE_STRING, &interface, DBUS_TYPE_INVALID)) goto err_out; iface = lookup_interface(interface, interfaces, error); if (*iface == NULL) goto err_out; if (!dbus_message_iter_open_container (&reply_iter, DBUS_TYPE_ARRAY, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &getall_dict)) goto getall_err; for (prop = (*iface)->props; prop && *prop; prop++) { prop_name = (*prop)->name; if ((*prop)->access == DBUS_PROP_READ || (*prop)->access == DBUS_PROP_READWRITE) { if (!dbus_message_iter_open_container (&getall_dict, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry)) goto getall_err; if (!dbus_message_iter_append_basic (&dict_entry, DBUS_TYPE_STRING, &prop_name)) goto getall_err; if (!dbus_message_iter_open_container (&dict_entry, DBUS_TYPE_VARIANT, (*prop)->type, &val_iter)) goto getall_err; if (!(*prop)->get(&val_iter)) goto getall_err; if (!dbus_message_iter_close_container (&dict_entry, &val_iter)) goto getall_err; if (!dbus_message_iter_close_container (&getall_dict, &dict_entry)) goto getall_err; } else { dbus_set_error(error, DBUS_ERROR_PROPERTY_READ_ONLY, "%s of %s from %s (write only?)", method, prop_name, interface); /** @todo@ check does write only make sense?? */ goto err_out; } } if (!dbus_message_iter_close_container (&reply_iter, &getall_dict)) goto getall_err; return true; /* DONE! */ } else if (strcmp(method, "Get") == 0) { if (!dbus_message_get_args (msg, error, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &prop_name, DBUS_TYPE_INVALID)) goto err_out; iface = lookup_interface(interface, interfaces, error); if (*iface == NULL) goto err_out; prop = lookup_property(prop_name, iface, error); if (*prop == NULL) goto err_out; if ((*prop)->access == DBUS_PROP_READ || (*prop)->access == DBUS_PROP_READWRITE) { DBusMessageIter variant_iter; if (!dbus_message_iter_open_container (&reply_iter, DBUS_TYPE_VARIANT, (*prop)->type, &variant_iter)) { dbus_set_error_const(error, DBUS_ERROR_FAILED, "Couldn't open Get container"); goto err_out; } retval = (*prop)->get(&variant_iter); if (retval == false || !dbus_message_iter_close_container(&reply_iter, &variant_iter)) { dbus_set_error_const(error, DBUS_ERROR_FAILED, "Couldn't close Get container"); goto err_out; } } else { dbus_set_error(error, DBUS_ERROR_PROPERTY_READ_ONLY, "%s of %s from %s (write only?)", method, prop_name, interface); /** @todo@ check does write only make sense?? */ goto err_out; } return true; /* DONE! */ } else if (strcmp(method, "Set") == 0) { DBusMessageIter iter_args; if (!dbus_message_iter_init(msg, &iter_args) || dbus_message_iter_get_arg_type(&iter_args) != DBUS_TYPE_STRING) { goto invalid_args; } dbus_message_iter_get_basic(&iter_args, &interface); if (!dbus_message_iter_next(&iter_args) || dbus_message_iter_get_arg_type(&iter_args) != DBUS_TYPE_STRING) { goto invalid_args; } dbus_message_iter_get_basic(&iter_args, &prop_name); if (!dbus_message_iter_next(&iter_args) || dbus_message_iter_get_arg_type(&iter_args) != DBUS_TYPE_VARIANT || dbus_message_iter_has_next(&iter_args)) { goto invalid_args; } iface = lookup_interface(interface, interfaces, error); if (*iface == NULL) goto err_out; prop = lookup_property(prop_name, iface, error); if (*prop == NULL) goto err_out; if ((*prop)->access == DBUS_PROP_WRITE || (*prop)->access == DBUS_PROP_READWRITE) { DBusMessageIter arg; dbus_message_iter_recurse(&iter_args, &arg); return (*prop)->set(&arg); /* DONE! */ } else { dbus_set_error(error, DBUS_ERROR_PROPERTY_READ_ONLY, "%s of %s from %s", method, prop_name, interface); goto err_out; } } else { dbus_set_error(error, DBUS_ERROR_UNKNOWN_METHOD, "Requested method: %s", method); } return retval; getall_err: dbus_set_error(error, DBUS_ERROR_FAILED, "GetAll container failure"); goto err_out; invalid_args: dbus_set_error(error, DBUS_ERROR_INVALID_ARGS, "Method %s", method); err_out: return retval; }
VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block, DBusMessage *, DBusConnection *, connection, DBusMessage *, message, int, timeout_milliseconds, DBusError *, error) { DBusMessage *reply = NULL; const char *service = dbus_message_get_destination(message); const char *member = dbus_message_get_member(message); VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block); if (STREQ(service, "org.freedesktop.machine1")) { if (getenv("FAIL_BAD_SERVICE")) { dbus_set_error_const(error, "org.freedesktop.systemd.badthing", "Something went wrong creating the machine"); } else { reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); } } else if (STREQ(service, "org.freedesktop.login1")) { char *supported = getenv("RESULT_SUPPORT"); DBusMessageIter iter; reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); dbus_message_iter_init_append(reply, &iter); if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &supported)) goto error; } else if (STREQ(service, "org.freedesktop.DBus") && STREQ(member, "ListActivatableNames")) { const char *svc1 = "org.foo.bar.wizz"; const char *svc2 = "org.freedesktop.machine1"; const char *svc3 = "org.freedesktop.login1"; DBusMessageIter iter; DBusMessageIter sub; reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); dbus_message_iter_init_append(reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub); if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc1)) goto error; if (!getenv("FAIL_NO_SERVICE") && !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc2)) goto error; if (!getenv("FAIL_NO_SERVICE") && !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc3)) goto error; dbus_message_iter_close_container(&iter, &sub); } else if (STREQ(service, "org.freedesktop.DBus") && STREQ(member, "ListNames")) { const char *svc1 = "org.foo.bar.wizz"; const char *svc2 = "org.freedesktop.systemd1"; const char *svc3 = "org.freedesktop.login1"; DBusMessageIter iter; DBusMessageIter sub; reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); dbus_message_iter_init_append(reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub); if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc1)) goto error; if ((!getenv("FAIL_NO_SERVICE") && !getenv("FAIL_NOT_REGISTERED")) && !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc2)) goto error; if ((!getenv("FAIL_NO_SERVICE") && !getenv("FAIL_NOT_REGISTERED")) && !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc3)) goto error; dbus_message_iter_close_container(&iter, &sub); } else { reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); } return reply; error: dbus_message_unref(reply); return NULL; }
dbus_bool_t _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, char **argv, char **envp, DBusSpawnChildSetupFunc child_setup, void *user_data, DBusError *error) { DBusBabysitter *sitter; HANDLE sitter_thread; int sitter_thread_id; _DBUS_ASSERT_ERROR_IS_CLEAR (error); *sitter_p = NULL; PING(); sitter = _dbus_babysitter_new (); if (sitter == NULL) { _DBUS_SET_OOM (error); return FALSE; } sitter->child_setup = child_setup; sitter->user_data = user_data; sitter->executable = _dbus_strdup (argv[0]); if (sitter->executable == NULL) { _DBUS_SET_OOM (error); goto out0; } PING(); if (!_dbus_full_duplex_pipe (&sitter->socket_to_babysitter, &sitter->socket_to_main, FALSE, error)) goto out0; sitter->sitter_watch = _dbus_watch_new (sitter->socket_to_babysitter, DBUS_WATCH_READABLE, TRUE, handle_watch, sitter, NULL); PING(); if (sitter->sitter_watch == NULL) { _DBUS_SET_OOM (error); goto out0; } PING(); if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch)) { _DBUS_SET_OOM (error); goto out0; } sitter->argc = protect_argv (argv, &sitter->argv); if (sitter->argc == -1) { _DBUS_SET_OOM (error); goto out0; } sitter->envp = envp; PING(); sitter_thread = (HANDLE) _beginthreadex (NULL, 0, babysitter, sitter, 0, &sitter_thread_id); if (sitter_thread == 0) { PING(); dbus_set_error_const (error, DBUS_ERROR_SPAWN_FORK_FAILED, "Failed to create new thread"); goto out0; } CloseHandle (sitter_thread); PING(); WaitForSingleObject (sitter->start_sync_event, INFINITE); PING(); if (sitter_p != NULL) *sitter_p = sitter; else _dbus_babysitter_unref (sitter); _DBUS_ASSERT_ERROR_IS_CLEAR (error); PING(); return TRUE; out0: _dbus_babysitter_unref (sitter); return FALSE; }
static DBusList* tokenize_command_line (const char *command_line, DBusError *error) { char current_quote; const char *p; DBusString current_token; DBusList *retval = NULL; dbus_bool_t quoted;; current_quote = '\0'; quoted = FALSE; p = command_line; if (!_dbus_string_init (¤t_token)) { _DBUS_SET_OOM (error); return NULL; } while (*p) { if (current_quote == '\\') { if (*p == '\n') { /* we append nothing; backslash-newline become nothing */ } else { if (!_dbus_string_append_byte (¤t_token, '\\') || !_dbus_string_append_byte (¤t_token, *p)) { _DBUS_SET_OOM (error); goto error; } } current_quote = '\0'; } else if (current_quote == '#') { /* Discard up to and including next newline */ while (*p && *p != '\n') ++p; current_quote = '\0'; if (*p == '\0') break; } else if (current_quote) { if (*p == current_quote && /* check that it isn't an escaped double quote */ !(current_quote == '"' && quoted)) { /* close the quote */ current_quote = '\0'; } /* Everything inside quotes, and the close quote, * gets appended literally. */ if (!_dbus_string_append_byte (¤t_token, *p)) { _DBUS_SET_OOM (error); goto error; } } else { switch (*p) { case '\n': if (!delimit_token (¤t_token, &retval, error)) goto error; _dbus_string_free (¤t_token); if (!_dbus_string_init (¤t_token)) { _DBUS_SET_OOM (error); goto init_error; } break; case ' ': case '\t': /* If the current token contains the previous char, delimit * the current token. A nonzero length * token should always contain the previous char. */ if (_dbus_string_get_length (¤t_token) > 0) { if (!delimit_token (¤t_token, &retval, error)) goto error; _dbus_string_free (¤t_token); if (!_dbus_string_init (¤t_token)) { _DBUS_SET_OOM (error); goto init_error; } } /* discard all unquoted blanks (don't add them to a token) */ break; /* single/double quotes are appended to the token, * escapes are maybe appended next time through the loop, * comment chars are never appended. */ case '\'': case '"': if (!_dbus_string_append_byte (¤t_token, *p)) { _DBUS_SET_OOM (error); goto error; } /* FALL THRU */ case '#': case '\\': current_quote = *p; break; default: /* Combines rules 4) and 6) - if we have a token, append to it, * otherwise create a new token. */ if (!_dbus_string_append_byte (¤t_token, *p)) { _DBUS_SET_OOM (error); goto error; } break; } } /* We need to count consecutive backslashes mod 2, * to detect escaped doublequotes. */ if (*p != '\\') quoted = FALSE; else quoted = !quoted; ++p; } if (!delimit_token (¤t_token, &retval, error)) goto error; if (current_quote) { dbus_set_error_const (error, DBUS_ERROR_INVALID_ARGS, "Unclosed quotes in command line"); goto error; } if (retval == NULL) { dbus_set_error_const (error, DBUS_ERROR_INVALID_ARGS, "No tokens found in command line"); goto error; } _dbus_string_free (¤t_token); return retval; error: _dbus_string_free (¤t_token); init_error: if (retval) { _dbus_list_foreach (&retval, (DBusForeachFunction) dbus_free, NULL); _dbus_list_clear (&retval); } return NULL; }
VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block, DBusMessage *, DBusConnection *, connection, DBusMessage *, message, int, timeout_milliseconds, DBusError *, error) { DBusMessage *reply = NULL; const char *service = dbus_message_get_destination(message); const char *member = dbus_message_get_member(message); size_t i; size_t nargs = 0; char **args = NULL; char *type = NULL; VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block); if (STREQ(service, "org.freedesktop.DBus") && STREQ(member, "ListNames")) { const char *svc1 = "org.foo.bar.wizz"; const char *svc2 = VIR_FIREWALL_FIREWALLD_SERVICE; DBusMessageIter iter; DBusMessageIter sub; reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); dbus_message_iter_init_append(reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub); if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc1)) goto error; if (!fwDisabled && !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc2)) goto error; dbus_message_iter_close_container(&iter, &sub); } else if (STREQ(service, VIR_FIREWALL_FIREWALLD_SERVICE) && STREQ(member, "passthrough")) { bool isAdd = false; bool doError = false; if (virDBusMessageDecode(message, "sa&s", &type, &nargs, &args) < 0) goto error; for (i = 0; i < nargs; i++) { /* Fake failure on the command with this IP addr */ if (STREQ(args[i], "-A")) { isAdd = true; } else if (isAdd && STREQ(args[i], "192.168.122.255")) { doError = true; } } if (fwBuf) { if (STREQ(type, "ipv4")) virBufferAddLit(fwBuf, IPTABLES_PATH); else if (STREQ(type, "ipv4")) virBufferAddLit(fwBuf, IP6TABLES_PATH); else virBufferAddLit(fwBuf, EBTABLES_PATH); } for (i = 0; i < nargs; i++) { if (fwBuf) { virBufferAddLit(fwBuf, " "); virBufferEscapeShell(fwBuf, args[i]); } } if (fwBuf) virBufferAddLit(fwBuf, "\n"); if (doError) { dbus_set_error_const(error, "org.firewalld.error", "something bad happened"); } else { if (nargs == 1 && STREQ(type, "ipv4") && STREQ(args[0], "-L")) { if (virDBusCreateReply(&reply, "s", TEST_FILTER_TABLE_LIST) < 0) goto error; } else if (nargs == 3 && STREQ(type, "ipv4") && STREQ(args[0], "-t") && STREQ(args[1], "nat") && STREQ(args[2], "-L")) { if (virDBusCreateReply(&reply, "s", TEST_NAT_TABLE_LIST) < 0) goto error; } else { if (virDBusCreateReply(&reply, "s", "success") < 0) goto error; } } } else { reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); } cleanup: VIR_FREE(type); for (i = 0; i < nargs; i++) VIR_FREE(args[i]); VIR_FREE(args); return reply; error: virDBusMessageUnref(reply); reply = NULL; if (error && !dbus_error_is_set(error)) dbus_set_error_const(error, "org.firewalld.error", "something unexpected happened"); goto cleanup; }
void Error::set( const char* name, const char* message ) { // _name = name; // _message = message; dbus_set_error_const(&_error, name, message); }