/** * Loads the message in the given message file. * * @param filename filename to load * @param data string to load message into * @returns #TRUE if the message was loaded */ dbus_bool_t dbus_internal_do_not_use_load_message_file (const DBusString *filename, DBusString *data) { dbus_bool_t retval; DBusError error = DBUS_ERROR_INIT; retval = FALSE; _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename)); if (!_dbus_file_get_contents (data, filename, &error)) { _dbus_warn ("Could not load message file %s: %s\n", _dbus_string_get_const_data (filename), error.message); dbus_error_free (&error); goto failed; } retval = TRUE; failed: return retval; }
/** * Unit test for dbus-userdb.c. * * @returns #TRUE on success. */ dbus_bool_t _dbus_userdb_test (const char *test_data_dir) { const DBusString *username; const DBusString *homedir; dbus_uid_t uid; unsigned long *group_ids; int n_group_ids, i; if (!_dbus_username_from_current_process (&username)) _dbus_assert_not_reached ("didn't get username"); if (!_dbus_homedir_from_current_process (&homedir)) _dbus_assert_not_reached ("didn't get homedir"); if (!_dbus_get_user_id (username, &uid)) _dbus_assert_not_reached ("didn't get uid"); if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids)) _dbus_assert_not_reached ("didn't get groups"); printf (" Current user: %s homedir: %s gids:", _dbus_string_get_const_data (username), _dbus_string_get_const_data (homedir)); for (i=0; i<n_group_ids; i++) printf(" %ld", group_ids[i]); printf ("\n"); dbus_free (group_ids); return TRUE; }
/** * reads the nonce from the nonce file and stores it in a string * * @param fname the file to read the nonce from * @param nonce returns the nonce. Must be an initialized string, the nonce will be appended. * @param error error object to report possible errors * @return FALSE iff reading the nonce fails (error is set then) */ dbus_bool_t _dbus_read_nonce (const DBusString *fname, DBusString *nonce, DBusError* error) { FILE *fp; char buffer[17]; size_t nread; buffer[sizeof buffer - 1] = '\0'; _DBUS_ASSERT_ERROR_IS_CLEAR (error); _dbus_verbose ("reading nonce from file: %s\n", _dbus_string_get_const_data (fname)); fp = fopen (_dbus_string_get_const_data (fname), "rb"); if (!fp) return FALSE; nread = fread (buffer, 1, sizeof buffer - 1, fp); fclose (fp); if (!nread) { dbus_set_error (error, DBUS_ERROR_FILE_NOT_FOUND, "Could not read nonce from file %s", _dbus_string_get_const_data (fname)); return FALSE; } if (!_dbus_string_append_len (nonce, buffer, sizeof buffer - 1 )) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return FALSE; } return TRUE; }
dbus_bool_t _dbus_stat (const DBusString *filename, DBusStat *statbuf, DBusError *error) { const char *filename_c; struct stat sb; _DBUS_ASSERT_ERROR_IS_CLEAR (error); filename_c = _dbus_string_get_const_data (filename); if (stat (filename_c, &sb) < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "%s", _dbus_strerror (errno)); return FALSE; } statbuf->mode = sb.st_mode; statbuf->nlink = sb.st_nlink; statbuf->uid = sb.st_uid; statbuf->gid = sb.st_gid; statbuf->size = sb.st_size; statbuf->atime = sb.st_atime; statbuf->mtime = sb.st_mtime; statbuf->ctime = sb.st_ctime; return TRUE; }
dbus_bool_t _dbus_user_at_console (const char *username, DBusError *error) { DBusString f; dbus_bool_t result; result = FALSE; if (!_dbus_string_init (&f)) { _DBUS_SET_OOM (error); return FALSE; } if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR)) { _DBUS_SET_OOM (error); goto out; } if (!_dbus_string_append (&f, username)) { _DBUS_SET_OOM (error); goto out; } result = _dbus_file_exists (_dbus_string_get_const_data (&f)); out: _dbus_string_free (&f); return result; }
static dbus_bool_t print_install_root() { DBusString runtime_prefix; if (!_dbus_string_init (&runtime_prefix)) { _dbus_assert_not_reached ("out of memory"); return FALSE; } if (!_dbus_get_install_root (&runtime_prefix)) { _dbus_assert_not_reached ("out of memory"); _dbus_string_free (&runtime_prefix); return FALSE; } if (_dbus_string_get_length (&runtime_prefix) == 0) { fprintf (stderr, "_dbus_get_install_root() failed\n"); _dbus_string_free (&runtime_prefix); return FALSE; } fprintf (stdout, "_dbus_get_install_root() returned '%s'\n", _dbus_string_get_const_data (&runtime_prefix)); _dbus_string_free (&runtime_prefix); return TRUE; }
static void randomly_set_extreme_ints (const DBusString *orig_data, DBusString *mutated) { int i; int byte_order; const char *d; dbus_uint32_t orig; static int which = 0; unsigned int extreme_ints[] = { _DBUS_INT_MAX, _DBUS_UINT_MAX, _DBUS_INT_MAX - 1, _DBUS_UINT_MAX - 1, _DBUS_INT_MAX - 2, _DBUS_UINT_MAX - 2, _DBUS_INT_MAX - 17, _DBUS_UINT_MAX - 17, _DBUS_INT_MAX / 2, _DBUS_INT_MAX / 3, _DBUS_UINT_MAX / 2, _DBUS_UINT_MAX / 3, 0, 1, 2, 3, (unsigned int) -1, (unsigned int) -2, (unsigned int) -3 }; if (orig_data != mutated) { _dbus_string_set_length (mutated, 0); if (!_dbus_string_copy (orig_data, 0, mutated, 0)) _dbus_assert_not_reached ("out of mem"); } if (_dbus_string_get_length (mutated) < 12) return; d = _dbus_string_get_const_data (mutated); if (!(*d == DBUS_LITTLE_ENDIAN || *d == DBUS_BIG_ENDIAN)) return; byte_order = *d; i = random_int_in_range (4, _dbus_string_get_length (mutated) - 8); i = _DBUS_ALIGN_VALUE (i, 4); orig = _dbus_demarshal_uint32 (mutated, byte_order, i, NULL); which = random_int_in_range (0, _DBUS_N_ELEMENTS (extreme_ints)); _dbus_assert (which >= 0); _dbus_assert (which < _DBUS_N_ELEMENTS (extreme_ints)); _dbus_marshal_set_uint32 (mutated, byte_order, i, extreme_ints[which]); }
/** * Creates a new transport for the "autostart" method. * This creates a client-side of a transport. * * @param error address where an error can be returned. * @returns a new transport, or #NULL on failure. */ static DBusTransport* _dbus_transport_new_for_autolaunch (DBusError *error) { DBusString address; DBusTransport *result = NULL; _DBUS_ASSERT_ERROR_IS_CLEAR (error); if (!_dbus_string_init (&address)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } if (!_dbus_get_autolaunch_address (&address, error)) { _DBUS_ASSERT_ERROR_IS_SET (error); goto out; } result = check_address (_dbus_string_get_const_data (&address), error); if (result == NULL) _DBUS_ASSERT_ERROR_IS_SET (error); else _DBUS_ASSERT_ERROR_IS_CLEAR (error); out: _dbus_string_free (&address); return result; }
/** * Tries loading the message in the given message file * and verifies that DBusMessageLoader can handle it. * * @param filename filename to load * @param expected_validity what the message has to be like to return #TRUE * @returns #TRUE if the message has the expected validity */ dbus_bool_t dbus_internal_do_not_use_try_message_file (const DBusString *filename, DBusValidity expected_validity) { DBusString data; dbus_bool_t retval; retval = FALSE; if (!_dbus_string_init (&data)) _dbus_assert_not_reached ("could not allocate string\n"); if (!dbus_internal_do_not_use_load_message_file (filename, &data)) goto failed; retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity); failed: if (!retval) { if (_dbus_string_get_length (&data) > 0) _dbus_verbose_bytes_of_string (&data, 0, _dbus_string_get_length (&data)); _dbus_warn ("Failed message loader test on %s\n", _dbus_string_get_const_data (filename)); } _dbus_string_free (&data); return retval; }
static void check_dirname (const char *filename, const char *dirname) { DBusString f, d; _dbus_string_init_const (&f, filename); if (!_dbus_string_init (&d)) _dbus_assert_not_reached ("no memory"); if (!_dbus_string_get_dirname (&f, &d)) _dbus_assert_not_reached ("no memory"); if (!_dbus_string_equal_c_str (&d, dirname)) { _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"\n", filename, _dbus_string_get_const_data (&d), dirname); exit (1); } _dbus_string_free (&d); }
static dbus_bool_t write_args_for_direction (DBusString *xml, const char *signature, dbus_bool_t in) { DBusTypeReader typereader; DBusString sigstr; int current_type; _dbus_string_init_const (&sigstr, signature); _dbus_type_reader_init_types_only (&typereader, &sigstr, 0); while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID) { const DBusString *subsig; int start, len; _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len); if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"", in ? "in" : "out")) goto oom; if (!_dbus_string_append_len (xml, _dbus_string_get_const_data (subsig) + start, len)) goto oom; if (!_dbus_string_append (xml, "\"/>\n")) goto oom; _dbus_type_reader_next (&typereader); } return TRUE; oom: return FALSE; }
DBusDirIter* _dbus_directory_open (const DBusString *filename, DBusError *error) { DIR *d; DBusDirIter *iter; const char *filename_c; _DBUS_ASSERT_ERROR_IS_CLEAR (error); filename_c = _dbus_string_get_const_data (filename); d = opendir (filename_c); if (d == NULL) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to read directory \"%s\": %s", filename_c, _dbus_strerror (errno)); return NULL; } iter = dbus_new0 (DBusDirIter, 1); if (iter == NULL) { closedir (d); dbus_set_error (error, DBUS_ERROR_NO_MEMORY, "Could not allocate memory for directory iterator"); return NULL; } iter->d = d; return iter; }
static dbus_bool_t do_check_nonce (int fd, const DBusString *nonce, DBusError *error) { DBusString buffer; DBusString p; size_t nleft; dbus_bool_t result; int n; _DBUS_ASSERT_ERROR_IS_CLEAR (error); nleft = 16; if ( !_dbus_string_init (&buffer) || !_dbus_string_init (&p) ) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (&p); _dbus_string_free (&buffer); return FALSE; } while (nleft) { n = _dbus_read_socket (fd, &p, nleft); if (n == -1 && _dbus_get_is_errno_eintr()) ; else if (n == -1 && _dbus_get_is_errno_eagain_or_ewouldblock()) _dbus_sleep_milliseconds (100); else if (n==-1) { dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%d)", fd ); _dbus_string_free (&p); _dbus_string_free (&buffer); return FALSE; } else if (!n) { _dbus_string_free (&p); _dbus_string_free (&buffer); dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%d)", fd ); return FALSE; } else { _dbus_string_append_len(&buffer, _dbus_string_get_const_data (&p), n); nleft -= n; } } result = _dbus_string_equal_len (&buffer, nonce, 16); if (!result) dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, "Nonces do not match, access denied (fd=%d)", fd ); _dbus_string_free (&p); _dbus_string_free (&buffer); return result; }
static void log_message (dbus_bool_t allow, const char *op, DBusString *data) { const char *mstr; #ifdef HAVE_LIBAUDIT int audit_fd; #endif if (allow) mstr = "ALLOWED"; else mstr = "DENIED"; #ifdef HAVE_LIBAUDIT audit_fd = bus_audit_get_fd (); if (audit_fd >= 0) { DBusString avc; if (!_dbus_string_init (&avc)) goto syslog; if (!_dbus_string_append_printf (&avc, "apparmor=\"%s\" operation=\"dbus_%s\" %s\n", mstr, op, _dbus_string_get_const_data (data))) { _dbus_string_free (&avc); goto syslog; } /* FIXME: need to change this to show real user */ audit_log_user_avc_message (audit_fd, AUDIT_USER_AVC, _dbus_string_get_const_data (&avc), NULL, NULL, NULL, getuid ()); _dbus_string_free (&avc); return; } syslog: #endif /* HAVE_LIBAUDIT */ syslog (LOG_USER | LOG_NOTICE, "apparmor=\"%s\" operation=\"dbus_%s\" %s\n", mstr, op, _dbus_string_get_const_data (data)); }
/** * Unit test for dbus-userdb.c. * * @returns #TRUE on success. */ dbus_bool_t _dbus_userdb_test (const char *test_data_dir) { const DBusString *username; const DBusString *homedir; dbus_uid_t uid; unsigned long *group_ids; int n_group_ids, i; DBusError error; if (!_dbus_username_from_current_process (&username)) _dbus_assert_not_reached ("didn't get username"); if (!_dbus_homedir_from_current_process (&homedir)) _dbus_assert_not_reached ("didn't get homedir"); if (!_dbus_get_user_id (username, &uid)) _dbus_assert_not_reached ("didn't get uid"); if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids)) _dbus_assert_not_reached ("didn't get groups"); printf (" Current user: %s homedir: %s gids:", _dbus_string_get_const_data (username), _dbus_string_get_const_data (homedir)); for (i=0; i<n_group_ids; i++) printf(" %ld", group_ids[i]); printf ("\n"); dbus_error_init (&error); printf ("Is Console user: %i\n", _dbus_is_console_user (uid, &error)); printf ("Invocation was OK: %s\n", error.message ? error.message : "yes"); dbus_error_free (&error); printf ("Is Console user 4711: %i\n", _dbus_is_console_user (4711, &error)); printf ("Invocation was OK: %s\n", error.message ? error.message : "yes"); dbus_error_free (&error); dbus_free (group_ids); return TRUE; }
static dbus_bool_t generate_many_bodies_inner (DBusMessageDataIter *iter, DBusMessage **message_p) { DBusMessage *message; DBusString signature; DBusString body; char byte_order; /* Keeping this small makes things go faster */ message = dbus_message_new_method_call ("o.z.F", "/", "o.z.B", "Nah"); if (message == NULL) _dbus_assert_not_reached ("oom"); byte_order = _dbus_header_get_byte_order (&message->header); set_reply_serial (message); if (!_dbus_string_init (&signature) || !_dbus_string_init (&body)) _dbus_assert_not_reached ("oom"); if (dbus_internal_do_not_use_generate_bodies (iter_get_sequence (iter), byte_order, &signature, &body)) { const char *v_SIGNATURE; v_SIGNATURE = _dbus_string_get_const_data (&signature); if (!_dbus_header_set_field_basic (&message->header, DBUS_HEADER_FIELD_SIGNATURE, DBUS_TYPE_SIGNATURE, &v_SIGNATURE)) _dbus_assert_not_reached ("oom"); if (!_dbus_string_move (&body, 0, &message->body, 0)) _dbus_assert_not_reached ("oom"); _dbus_marshal_set_uint32 (&message->header.data, BODY_LENGTH_OFFSET, _dbus_string_get_length (&message->body), byte_order); *message_p = message; } else { dbus_message_unref (message); *message_p = NULL; } _dbus_string_free (&signature); _dbus_string_free (&body); return *message_p != NULL; }
static void check_two_pid_descriptors (const DBusString *pid_fd, const char *extra_arg) { if (_dbus_string_get_length (pid_fd) > 0) { fprintf (stderr, "--%s specified but printing pid to %s already requested\n", extra_arg, _dbus_string_get_const_data (pid_fd)); exit (1); } }
static void check_two_addresses (const DBusString *address, const char *extra_arg) { if (_dbus_string_get_length (address) > 0) { fprintf (stderr, "--%s specified but address %s already requested\n", extra_arg, _dbus_string_get_const_data (address)); exit (1); } }
BusService* bus_registry_lookup (BusRegistry *registry, const DBusString *service_name) { BusService *service; service = _dbus_hash_table_lookup_string (registry->service_hash, _dbus_string_get_const_data (service_name)); return service; }
static void check_two_config_files (const DBusString *config_file, const char *extra_arg) { if (_dbus_string_get_length (config_file) > 0) { fprintf (stderr, "--%s specified but configuration file %s already requested\n", extra_arg, _dbus_string_get_const_data (config_file)); exit (1); } }
/** * Checks that the given range of the string is a valid object path * name in the D-Bus protocol. Part of the validation ensures that * the object path contains only ASCII. * * @todo this is inconsistent with most of DBusString in that * it allows a start,len range that extends past the string end. * * @todo change spec to disallow more things, such as spaces in the * path name * * @param str the string * @param start first byte index to check * @param len number of bytes to check * @returns #TRUE if the byte range exists and is a valid name */ dbus_bool_t _dbus_validate_path (const DBusString *str, int start, int len) { const unsigned char *s; const unsigned char *end; const unsigned char *last_slash; _dbus_assert (start >= 0); _dbus_assert (len >= 0); _dbus_assert (start <= _dbus_string_get_length (str)); if (len > _dbus_string_get_length (str) - start) return FALSE; if (len == 0) return FALSE; s = _dbus_string_get_const_data (str) + start; end = s + len; if (*s != '/') return FALSE; last_slash = s; ++s; while (s != end) { if (*s == '/') { if ((s - last_slash) < 2) return FALSE; /* no empty path components allowed */ last_slash = s; } else { if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s))) return FALSE; } ++s; } if ((end - last_slash) < 2 && len > 1) return FALSE; /* trailing slash not allowed unless the string is "/" */ return TRUE; }
int main (int argc, char **argv) { #ifdef DBUS_BUILD_TESTS const char *dir; DBusString config_file; if (argc > 1) dir = argv[1]; else dir = _dbus_getenv ("DBUS_TEST_DATA"); if (dir == NULL) { fprintf (stderr, "Must specify test data directory as argv[1] or in DBUS_TEST_DATA env variable\n"); return 1; } printf ("%s: Running launch helper OOM checks\n", argv[0]); if (!_dbus_string_init (&config_file)) return 1; if (!_dbus_string_append (&config_file, dir)) return 1; if (!_dbus_string_append (&config_file, "/valid-config-files-system/debug-allow-all-pass.conf")) return 1; /* use a config file that will actually work... */ _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&config_file)); _dbus_string_free (&config_file); if (!_dbus_test_oom_handling ("dbus-daemon-launch-helper", bus_activation_helper_oom_test, "org.freedesktop.DBus.TestSuiteEchoService")) die ("OOM failed"); test_post_hook (argv[0]); printf ("%s: Success\n", argv[0]); return 0; #else /* DBUS_BUILD_TESTS */ printf ("Not compiled with test support\n"); return 0; #endif }
static void test_hex_roundtrip (const unsigned char *data, int len) { DBusString orig; DBusString encoded; DBusString decoded; int end; if (len < 0) len = strlen (data); if (!_dbus_string_init (&orig)) _dbus_assert_not_reached ("could not init string"); if (!_dbus_string_init (&encoded)) _dbus_assert_not_reached ("could not init string"); if (!_dbus_string_init (&decoded)) _dbus_assert_not_reached ("could not init string"); if (!_dbus_string_append_len (&orig, data, len)) _dbus_assert_not_reached ("couldn't append orig data"); if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0)) _dbus_assert_not_reached ("could not encode"); if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0)) _dbus_assert_not_reached ("could not decode"); _dbus_assert (_dbus_string_get_length (&encoded) == end); if (!_dbus_string_equal (&orig, &decoded)) { const char *s; printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n", _dbus_string_get_length (&orig), _dbus_string_get_length (&encoded), _dbus_string_get_length (&decoded)); printf ("Original: %s\n", data); s = _dbus_string_get_const_data (&decoded); printf ("Decoded: %s\n", s); _dbus_assert_not_reached ("original string not the same as string decoded from hex"); } _dbus_string_free (&orig); _dbus_string_free (&encoded); _dbus_string_free (&decoded); }
dbus_bool_t _dbus_write_pid_file (const DBusString *filename, unsigned long pid, DBusError *error) { const char *cfilename; int fd; FILE *f; cfilename = _dbus_string_get_const_data (filename); fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644); if (fd < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to open \"%s\": %s", cfilename, _dbus_strerror (errno)); return FALSE; } if ((f = fdopen (fd, "w")) == NULL) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno)); _dbus_close (fd, NULL); return FALSE; } if (fprintf (f, "%lu\n", pid) < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to write to \"%s\": %s", cfilename, _dbus_strerror (errno)); fclose (f); return FALSE; } if (fclose (f) == EOF) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to close \"%s\": %s", cfilename, _dbus_strerror (errno)); return FALSE; } return TRUE; }
/* * Return TRUE on successful check, FALSE on OOM. * Set *is_supported to whether AA has D-Bus features. */ static dbus_bool_t _bus_apparmor_detect_aa_dbus_support (dbus_bool_t *is_supported) { int mask_file; DBusString aa_dbus; char *aa_securityfs = NULL; dbus_bool_t retval = FALSE; *is_supported = FALSE; if (!_dbus_string_init (&aa_dbus)) return FALSE; if (aa_find_mountpoint (&aa_securityfs) != 0) goto out; /* * John Johansen has confirmed that the mainline kernel will not have * the apparmorfs/features/dbus/mask file until the mainline kernel * has AppArmor getpeersec support. */ if (!_dbus_string_append (&aa_dbus, aa_securityfs) || !_dbus_string_append (&aa_dbus, "/features/dbus/mask")) goto out; /* We need to open() the flag file, not just stat() it, because AppArmor * does not mediate stat() in the apparmorfs. If you have a * dbus-daemon inside an LXC container, with insufficiently broad * AppArmor privileges to do its own AppArmor mediation, the desired * result is that it behaves as if AppArmor was not present; but a stat() * here would succeed, and result in it trying and failing to do full * mediation. https://bugs.launchpad.net/ubuntu/+source/dbus/+bug/1238267 */ mask_file = open (_dbus_string_get_const_data (&aa_dbus), O_RDONLY | O_CLOEXEC); if (mask_file != -1) { *is_supported = TRUE; close (mask_file); } retval = TRUE; out: free (aa_securityfs); _dbus_string_free (&aa_dbus); return retval; }
/** * Checks that the given range of the string is a valid member name * in the D-Bus protocol. This includes a length restriction, etc., * see the specification. * * @todo this is inconsistent with most of DBusString in that * it allows a start,len range that extends past the string end. * * @param str the string * @param start first byte index to check * @param len number of bytes to check * @returns #TRUE if the byte range exists and is a valid name */ dbus_bool_t _dbus_validate_member (const DBusString *str, int start, int len) { const unsigned char *s; const unsigned char *end; const unsigned char *member; _dbus_assert (start >= 0); _dbus_assert (len >= 0); _dbus_assert (start <= _dbus_string_get_length (str)); if (len > _dbus_string_get_length (str) - start) return FALSE; if (len > DBUS_MAXIMUM_NAME_LENGTH) return FALSE; if (len == 0) return FALSE; member = _dbus_string_get_const_data (str) + start; end = member + len; s = member; /* check special cases of first char so it doesn't have to be done * in the loop. Note we know len > 0 */ if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s))) return FALSE; else ++s; while (s != end) { if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s))) { return FALSE; } ++s; } return TRUE; }
static unsigned int get_random_seed (void) { DBusString bytes; unsigned int seed; int fd; const char *s; seed = 0; if (!_dbus_string_init (&bytes)) exit (1); fd = open ("/dev/urandom", O_RDONLY); if (fd < 0) goto use_fallback; if (_dbus_read (fd, &bytes, 4) != 4) goto use_fallback; close (fd); s = _dbus_string_get_const_data (&bytes); seed = * (unsigned int*) s; goto out; use_fallback: { long tv_usec; fprintf (stderr, "could not open/read /dev/urandom, using current time for seed\n"); _dbus_get_current_time (NULL, &tv_usec); seed = tv_usec; } out: _dbus_string_free (&bytes); return seed; }
/** * write data to a pipe. * * @param pipe the pipe instance * @param buffer the buffer to write data from * @param start the first byte in the buffer to write * @param len the number of bytes to try to write * @param error error return * @returns the number of bytes written or -1 on error */ int _dbus_pipe_write (DBusPipe *pipe, const DBusString *buffer, int start, int len, DBusError *error) { const char *buffer_c = _dbus_string_get_const_data (buffer); int written; written = _write (pipe->fd, buffer_c + start, len); if (written >= 0) return written; dbus_set_error (error, _dbus_error_from_system_errno (), "Writing to pipe: %s", _dbus_strerror_from_errno ()); return -1; }
dbus_bool_t _dbus_delete_directory (const DBusString *filename, DBusError *error) { const char *filename_c; _DBUS_ASSERT_ERROR_IS_CLEAR (error); filename_c = _dbus_string_get_const_data (filename); if (rmdir (filename_c) != 0) { dbus_set_error (error, DBUS_ERROR_FAILED, "Failed to remove directory %s: %s\n", filename_c, _dbus_strerror (errno)); return FALSE; } return TRUE; }
static dbus_bool_t check_file_valid (DBusString *full_path, Validity validity) { dbus_bool_t retval; if (validity == VALID) printf ("Testing valid file:\n"); else if (validity == INVALID) printf ("Testing invalid file:\n"); else printf ("Testing unknown file:\n"); /* print the filename, just so we match the other output */ printf (" %s\n", _dbus_string_get_const_data (full_path)); /* only test one file */ retval = do_load (full_path, validity, TRUE); return retval; }