1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-BUS implementation)
4 * Copyright (C) 2002 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-sysdeps.h"
26 #include <sys/types.h>
34 #include <sys/socket.h>
54 * @addtogroup DBusInternalsUtils
58 * Aborts the program with SIGABRT (dumping core).
64 _exit (1); /* in case someone manages to ignore SIGABRT */
68 * Wrapper for getenv().
70 * @param varname name of environment variable
71 * @returns value of environment variable or #NULL if unset
74 _dbus_getenv (const char *varname)
76 return getenv (varname);
80 * Thin wrapper around the read() system call that appends
81 * the data it reads to the DBusString buffer. It appends
82 * up to the given count, and returns the same value
83 * and same errno as read(). The only exception is that
84 * _dbus_read() handles EINTR for you.
86 * @param fd the file descriptor to read from
87 * @param buffer the buffer to append data to
88 * @param count the amount of data to read
89 * @returns the number of bytes read or -1
100 _dbus_assert (count >= 0);
102 start = _dbus_string_get_length (buffer);
104 if (!_dbus_string_lengthen (buffer, count))
110 _dbus_string_get_data_len (buffer, &data, start, count);
114 bytes_read = read (fd, data, count);
122 /* put length back (note that this doesn't actually realloc anything) */
123 _dbus_string_set_length (buffer, start);
129 /* put length back (doesn't actually realloc) */
130 _dbus_string_set_length (buffer, start + bytes_read);
134 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
142 * Thin wrapper around the write() system call that writes a part of a
143 * DBusString and handles EINTR for you.
145 * @param fd the file descriptor to write
146 * @param buffer the buffer to write data from
147 * @param start the first byte in the buffer to write
148 * @param len the number of bytes to try to write
149 * @returns the number of bytes written or -1 on error
153 const DBusString *buffer,
160 _dbus_string_get_const_data_len (buffer, &data, start, len);
164 bytes_written = write (fd, data, len);
166 if (bytes_written < 0 && errno == EINTR)
170 if (bytes_written > 0)
171 _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
174 return bytes_written;
178 * Like _dbus_write() but will use writev() if possible
179 * to write both buffers in sequence. The return value
180 * is the number of bytes written in the first buffer,
181 * plus the number written in the second. If the first
182 * buffer is written successfully and an error occurs
183 * writing the second, the number of bytes in the first
184 * is returned (i.e. the error is ignored), on systems that
185 * don't have writev. Handles EINTR for you.
186 * The second buffer may be #NULL.
188 * @param fd the file descriptor
189 * @param buffer1 first buffer
190 * @param start1 first byte to write in first buffer
191 * @param len1 number of bytes to write from first buffer
192 * @param buffer2 second buffer, or #NULL
193 * @param start2 first byte to write in second buffer
194 * @param len2 number of bytes to write in second buffer
195 * @returns total bytes written from both buffers, or -1 on error
198 _dbus_write_two (int fd,
199 const DBusString *buffer1,
202 const DBusString *buffer2,
206 _dbus_assert (buffer1 != NULL);
207 _dbus_assert (start1 >= 0);
208 _dbus_assert (start2 >= 0);
209 _dbus_assert (len1 >= 0);
210 _dbus_assert (len2 >= 0);
214 struct iovec vectors[2];
219 _dbus_string_get_const_data_len (buffer1, &data1, start1, len1);
222 _dbus_string_get_const_data_len (buffer2, &data2, start2, len2);
230 vectors[0].iov_base = (char*) data1;
231 vectors[0].iov_len = len1;
232 vectors[1].iov_base = (char*) data2;
233 vectors[1].iov_len = len2;
237 bytes_written = writev (fd,
241 if (bytes_written < 0 && errno == EINTR)
244 return bytes_written;
246 #else /* HAVE_WRITEV */
250 ret1 = _dbus_write (fd, buffer1, start1, len1);
251 if (ret1 == len1 && buffer2 != NULL)
253 ret2 = _dbus_write (fd, buffer2, start2, len2);
255 ret2 = 0; /* we can't report an error as the first write was OK */
262 #endif /* !HAVE_WRITEV */
266 * Creates a socket and connects it to the UNIX domain socket at the
267 * given path. The connection fd is returned, and is set up as
270 * @param path the path to UNIX domain socket
271 * @param result return location for error code
272 * @returns connection file descriptor or -1 on error
275 _dbus_connect_unix_socket (const char *path,
276 DBusResultCode *result)
279 struct sockaddr_un addr;
281 fd = socket (AF_LOCAL, SOCK_STREAM, 0);
285 dbus_set_result (result,
286 _dbus_result_from_errno (errno));
288 _dbus_verbose ("Failed to create socket: %s\n",
289 _dbus_strerror (errno));
295 addr.sun_family = AF_LOCAL;
296 strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
297 addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
299 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
301 dbus_set_result (result,
302 _dbus_result_from_errno (errno));
304 _dbus_verbose ("Failed to connect to socket %s: %s\n",
305 path, _dbus_strerror (errno));
313 if (!_dbus_set_fd_nonblocking (fd, result))
325 * Creates a socket and binds it to the given path,
326 * then listens on the socket. The socket is
327 * set to be nonblocking.
329 * @param path the socket name
330 * @param result return location for errors
331 * @returns the listening file descriptor or -1 on error
334 _dbus_listen_unix_socket (const char *path,
335 DBusResultCode *result)
338 struct sockaddr_un addr;
340 listen_fd = socket (AF_LOCAL, SOCK_STREAM, 0);
344 dbus_set_result (result, _dbus_result_from_errno (errno));
345 _dbus_verbose ("Failed to create socket \"%s\": %s\n",
346 path, _dbus_strerror (errno));
351 addr.sun_family = AF_LOCAL;
352 strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
353 addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
355 if (bind (listen_fd, (struct sockaddr*) &addr, SUN_LEN (&addr)) < 0)
357 dbus_set_result (result, _dbus_result_from_errno (errno));
358 _dbus_verbose ("Failed to bind socket \"%s\": %s\n",
359 path, _dbus_strerror (errno));
364 if (listen (listen_fd, 30 /* backlog */) < 0)
366 dbus_set_result (result, _dbus_result_from_errno (errno));
367 _dbus_verbose ("Failed to listen on socket \"%s\": %s\n",
368 path, _dbus_strerror (errno));
373 if (!_dbus_set_fd_nonblocking (listen_fd, result))
382 /* try to read a single byte and return #TRUE if we read it
383 * and it's equal to nul.
386 read_credentials_byte (int client_fd,
387 DBusResultCode *result)
393 bytes_read = read (client_fd, buf, 1);
400 dbus_set_result (result, _dbus_result_from_errno (errno));
401 _dbus_verbose ("Failed to read credentials byte: %s\n",
402 _dbus_strerror (errno));
406 else if (bytes_read == 0)
408 dbus_set_result (result, DBUS_RESULT_IO_ERROR);
409 _dbus_verbose ("EOF reading credentials byte\n");
414 _dbus_assert (bytes_read == 1);
418 dbus_set_result (result, DBUS_RESULT_FAILED);
419 _dbus_verbose ("Credentials byte was not nul\n");
423 _dbus_verbose ("read credentials byte\n");
430 write_credentials_byte (int server_fd,
431 DBusResultCode *result)
434 char buf[1] = { '\0' };
438 bytes_written = write (server_fd, buf, 1);
440 if (bytes_written < 0 && errno == EINTR)
443 if (bytes_written < 0)
445 dbus_set_result (result, _dbus_result_from_errno (errno));
446 _dbus_verbose ("Failed to write credentials byte: %s\n",
447 _dbus_strerror (errno));
450 else if (bytes_written == 0)
452 dbus_set_result (result, DBUS_RESULT_IO_ERROR);
453 _dbus_verbose ("wrote zero bytes writing credentials byte\n");
458 _dbus_assert (bytes_written == 1);
459 _dbus_verbose ("wrote credentials byte\n");
465 * Reads a single byte which must be nul (an error occurs otherwise),
466 * and reads unix credentials if available. Fills in pid/uid/gid with
467 * -1 if no credentials are available. Return value indicates whether
468 * a byte was read, not whether we got valid credentials. On some
469 * systems, such as Linux, reading/writing the byte isn't actually
470 * required, but we do it anyway just to avoid multiple codepaths.
472 * Fails if no byte is available, so you must select() first.
474 * The point of the byte is that on some systems we have to
475 * use sendmsg()/recvmsg() to transmit credentials.
477 * @param client_fd the client file descriptor
478 * @param credentials struct to fill with credentials of client
479 * @param result location to store result code
480 * @returns #TRUE on success
483 _dbus_read_credentials_unix_socket (int client_fd,
484 DBusCredentials *credentials,
485 DBusResultCode *result)
487 credentials->pid = -1;
488 credentials->uid = -1;
489 credentials->gid = -1;
492 if (read_credentials_byte (client_fd, result))
495 int cr_len = sizeof (cr);
497 if (getsockopt (client_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 &&
498 cr_len == sizeof (cr))
500 credentials->pid = cr.pid;
501 credentials->uid = cr.uid;
502 credentials->gid = cr.gid;
503 _dbus_verbose ("Got credentials pid %d uid %d gid %d\n",
510 _dbus_verbose ("Failed to getsockopt() credentials, returned len %d/%d: %s\n",
511 cr_len, (int) sizeof (cr), _dbus_strerror (errno));
518 #else /* !SO_PEERCRED */
519 _dbus_verbose ("Socket credentials not supported on this OS\n");
525 * Sends a single nul byte with our UNIX credentials as ancillary
526 * data. Returns #TRUE if the data was successfully written. On
527 * systems that don't support sending credentials, just writes a byte,
528 * doesn't send any credentials. On some systems, such as Linux,
529 * reading/writing the byte isn't actually required, but we do it
530 * anyway just to avoid multiple codepaths.
532 * Fails if no byte can be written, so you must select() first.
534 * The point of the byte is that on some systems we have to
535 * use sendmsg()/recvmsg() to transmit credentials.
537 * @param server_fd file descriptor for connection to server
538 * @param result return location for error code
539 * @returns #TRUE if the byte was sent
542 _dbus_send_credentials_unix_socket (int server_fd,
543 DBusResultCode *result)
545 if (write_credentials_byte (server_fd, result))
552 * Accepts a connection on a listening socket.
553 * Handles EINTR for you.
555 * @param listen_fd the listen file descriptor
556 * @returns the connection fd of the client, or -1 on error
559 _dbus_accept (int listen_fd)
564 client_fd = accept (listen_fd, NULL, NULL);
578 * @addtogroup DBusString
583 * Appends an integer to a DBusString.
585 * @param str the string
586 * @param value the integer value
587 * @returns #FALSE if not enough memory or other failure.
590 _dbus_string_append_int (DBusString *str,
593 /* this calculation is from comp.lang.c faq */
594 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
599 orig_len = _dbus_string_get_length (str);
601 if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
604 _dbus_string_get_data_len (str, &buf, orig_len, MAX_LONG_LEN);
606 snprintf (buf, MAX_LONG_LEN, "%ld", value);
615 _dbus_string_shorten (str, MAX_LONG_LEN - i);
621 * Appends a double to a DBusString.
623 * @param str the string
624 * @param value the floating point value
625 * @returns #FALSE if not enough memory or other failure.
628 _dbus_string_append_double (DBusString *str,
631 #define MAX_DOUBLE_LEN 64 /* this is completely made up :-/ */
636 orig_len = _dbus_string_get_length (str);
638 if (!_dbus_string_lengthen (str, MAX_DOUBLE_LEN))
641 _dbus_string_get_data_len (str, &buf, orig_len, MAX_DOUBLE_LEN);
643 snprintf (buf, MAX_LONG_LEN, "%g", value);
652 _dbus_string_shorten (str, MAX_DOUBLE_LEN - i);
658 * Parses an integer contained in a DBusString. Either return parameter
659 * may be #NULL if you aren't interested in it. The integer is parsed
660 * and stored in value_return. Return parameters are not initialized
661 * if the function returns #FALSE.
663 * @param str the string
664 * @param start the byte index of the start of the integer
665 * @param value_return return location of the integer value or #NULL
666 * @param end_return return location of the end of the integer, or #NULL
667 * @returns #TRUE on success
670 _dbus_string_parse_int (const DBusString *str,
679 _dbus_string_get_const_data_len (str, &p, start,
680 _dbus_string_get_length (str) - start);
684 v = strtol (p, &end, 0);
685 if (end == NULL || end == p || errno != 0)
691 *end_return = (end - p);
697 * Parses a floating point number contained in a DBusString. Either
698 * return parameter may be #NULL if you aren't interested in it. The
699 * integer is parsed and stored in value_return. Return parameters are
700 * not initialized if the function returns #FALSE.
702 * @todo this function is currently locale-dependent. Should
703 * ask alexl to relicense g_ascii_strtod() code and put that in
704 * here instead, so it's locale-independent.
706 * @param str the string
707 * @param start the byte index of the start of the float
708 * @param value_return return location of the float value or #NULL
709 * @param end_return return location of the end of the float, or #NULL
710 * @returns #TRUE on success
713 _dbus_string_parse_double (const DBusString *str,
715 double *value_return,
722 _dbus_warn ("_dbus_string_parse_double() needs to be made locale-independent\n");
724 _dbus_string_get_const_data_len (str, &p, start,
725 _dbus_string_get_length (str) - start);
729 v = strtod (p, &end);
730 if (end == NULL || end == p || errno != 0)
736 *end_return = (end - p);
742 * Gets the credentials corresponding to the given username.
744 * @param username the username
745 * @param credentials credentials to fill in
746 * @returns #TRUE if the username existed and we got some credentials
749 _dbus_credentials_from_username (const DBusString *username,
750 DBusCredentials *credentials)
752 const char *username_c_str;
754 credentials->pid = -1;
755 credentials->uid = -1;
756 credentials->gid = -1;
758 _dbus_string_get_const_data (username, &username_c_str);
760 #ifdef HAVE_GETPWNAM_R
768 result = getpwnam_r (username_c_str, &p_str, buf, sizeof (buf),
771 if (result == 0 && p == &p_str)
773 credentials->uid = p->pw_uid;
774 credentials->gid = p->pw_gid;
776 _dbus_verbose ("Username %s has uid %d gid %d\n",
777 username_c_str, credentials->uid, credentials->gid);
782 _dbus_verbose ("User %s unknown\n", username_c_str);
786 #else /* ! HAVE_GETPWNAM_R */
788 /* I guess we're screwed on thread safety here */
791 p = getpwnam (username_c_str);
795 credentials->uid = p->pw_uid;
796 credentials->gid = p->pw_gid;
798 _dbus_verbose ("Username %s has uid %d gid %d\n",
799 username_c_str, credentials->uid, credentials->gid);
804 _dbus_verbose ("User %s unknown\n", username_c_str);
812 * Gets credentials from a UID string. (Parses a string to a UID
813 * and converts to a DBusCredentials.)
815 * @param uid_str the UID in string form
816 * @param credentials credentials to fill in
817 * @returns #TRUE if successfully filled in some credentials
820 _dbus_credentials_from_uid_string (const DBusString *uid_str,
821 DBusCredentials *credentials)
826 credentials->pid = -1;
827 credentials->uid = -1;
828 credentials->gid = -1;
830 if (_dbus_string_get_length (uid_str) == 0)
832 _dbus_verbose ("UID string was zero length\n");
838 if (!_dbus_string_parse_int (uid_str, 0, &uid,
841 _dbus_verbose ("could not parse string as a UID\n");
845 if (end != _dbus_string_get_length (uid_str))
847 _dbus_verbose ("string contained trailing stuff after UID\n");
851 credentials->uid = uid;
857 * Gets the credentials of the current process.
859 * @param credentials credentials to fill in.
862 _dbus_credentials_from_current_process (DBusCredentials *credentials)
864 credentials->pid = getpid ();
865 credentials->uid = getuid ();
866 credentials->gid = getgid ();
870 * Checks whether the provided_credentials are allowed to log in
871 * as the expected_credentials.
873 * @param expected_credentials credentials we're trying to log in as
874 * @param provided_credentials credentials we have
875 * @returns #TRUE if we can log in
878 _dbus_credentials_match (const DBusCredentials *expected_credentials,
879 const DBusCredentials *provided_credentials)
881 if (provided_credentials->uid < 0)
883 else if (expected_credentials->uid < 0)
885 else if (provided_credentials->uid == 0)
887 else if (provided_credentials->uid == expected_credentials->uid)
894 * Appends the uid of the current process to the given string.
896 * @param str the string to append to
897 * @returns #TRUE on success
900 _dbus_string_append_our_uid (DBusString *str)
902 return _dbus_string_append_int (str, getuid ());
907 * Wrapper for poll().
909 * @todo need a fallback implementation using select()
911 * @param fds the file descriptors to poll
912 * @param n_fds number of descriptors in the array
913 * @param timeout_milliseconds timeout or -1 for infinite
914 * @returns numbers of fds with revents, or <0 on error
917 _dbus_poll (DBusPollFD *fds,
919 int timeout_milliseconds)
922 /* This big thing is a constant expression and should get optimized
923 * out of existence. So it's more robust than a configure check at
926 if (_DBUS_POLLIN == POLLIN &&
927 _DBUS_POLLPRI == POLLPRI &&
928 _DBUS_POLLOUT == POLLOUT &&
929 _DBUS_POLLERR == POLLERR &&
930 _DBUS_POLLHUP == POLLHUP &&
931 _DBUS_POLLNVAL == POLLNVAL &&
932 sizeof (DBusPollFD) == sizeof (struct pollfd) &&
933 _DBUS_STRUCT_OFFSET (DBusPollFD, fd) ==
934 _DBUS_STRUCT_OFFSET (struct pollfd, fd) &&
935 _DBUS_STRUCT_OFFSET (DBusPollFD, events) ==
936 _DBUS_STRUCT_OFFSET (struct pollfd, events) &&
937 _DBUS_STRUCT_OFFSET (DBusPollFD, revents) ==
938 _DBUS_STRUCT_OFFSET (struct pollfd, revents))
940 return poll ((struct pollfd*) fds,
942 timeout_milliseconds);
946 /* We have to convert the DBusPollFD to an array of
947 * struct pollfd, poll, and convert back.
949 _dbus_warn ("didn't implement poll() properly for this system yet\n");
952 #else /* ! HAVE_POLL */
953 _dbus_warn ("need to implement select() fallback for systems with no poll()\n");
958 /** nanoseconds in a second */
959 #define NANOSECONDS_PER_SECOND 1000000000
960 /** microseconds in a second */
961 #define MICROSECONDS_PER_SECOND 1000000
962 /** milliseconds in a second */
963 #define MILLISECONDS_PER_SECOND 1000
964 /** nanoseconds in a millisecond */
965 #define NANOSECONDS_PER_MILLISECOND 1000000
966 /** microseconds in a millisecond */
967 #define MICROSECONDS_PER_MILLISECOND 1000
970 * Sleeps the given number of milliseconds.
971 * @param milliseconds number of milliseconds
974 _dbus_sleep_milliseconds (int milliseconds)
976 #ifdef HAVE_NANOSLEEP
980 req.tv_sec = milliseconds / MILLISECONDS_PER_SECOND;
981 req.tv_nsec = (milliseconds % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
985 while (nanosleep (&req, &rem) < 0 && errno == EINTR)
987 #elif defined (HAVE_USLEEP)
988 usleep (milliseconds * MICROSECONDS_PER_MILLISECOND);
989 #else /* ! HAVE_USLEEP */
990 sleep (MAX (milliseconds / 1000, 1));
995 * Get current time, as in gettimeofday().
997 * @param tv_sec return location for number of seconds
998 * @param tv_usec return location for number of microseconds (thousandths)
1001 _dbus_get_current_time (long *tv_sec,
1006 gettimeofday (&t, NULL);
1011 *tv_usec = t.tv_usec;
1015 * Appends the contents of the given file to the string,
1016 * returning result code. At the moment, won't open a file
1017 * more than a megabyte in size.
1019 * @param str the string to append to
1020 * @param filename filename to load
1024 _dbus_file_get_contents (DBusString *str,
1025 const DBusString *filename)
1031 const char *filename_c;
1033 _dbus_string_get_const_data (filename, &filename_c);
1035 /* O_BINARY useful on Cygwin */
1036 fd = open (filename_c, O_RDONLY | O_BINARY);
1038 return _dbus_result_from_errno (errno);
1040 if (fstat (fd, &sb) < 0)
1042 DBusResultCode result;
1044 result = _dbus_result_from_errno (errno); /* prior to close() */
1046 _dbus_verbose ("fstat() failed: %s",
1047 _dbus_strerror (errno));
1054 if (sb.st_size > _DBUS_ONE_MEGABYTE)
1056 _dbus_verbose ("File size %lu is too large.\n",
1057 (unsigned long) sb.st_size);
1059 return DBUS_RESULT_FAILED;
1063 orig_len = _dbus_string_get_length (str);
1064 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
1068 while (total < (int) sb.st_size)
1070 bytes_read = _dbus_read (fd, str,
1071 sb.st_size - total);
1072 if (bytes_read <= 0)
1074 DBusResultCode result;
1076 result = _dbus_result_from_errno (errno); /* prior to close() */
1078 _dbus_verbose ("read() failed: %s",
1079 _dbus_strerror (errno));
1082 _dbus_string_set_length (str, orig_len);
1086 total += bytes_read;
1090 return DBUS_RESULT_SUCCESS;
1092 else if (sb.st_size != 0)
1094 _dbus_verbose ("Can only open regular files at the moment.\n");
1096 return DBUS_RESULT_FAILED;
1101 return DBUS_RESULT_SUCCESS;
1106 * Appends the given filename to the given directory.
1108 * @param dir the directory name
1109 * @param next_component the filename
1110 * @returns #TRUE on success
1113 _dbus_concat_dir_and_file (DBusString *dir,
1114 const DBusString *next_component)
1116 dbus_bool_t dir_ends_in_slash;
1117 dbus_bool_t file_starts_with_slash;
1119 if (_dbus_string_get_length (dir) == 0 ||
1120 _dbus_string_get_length (next_component) == 0)
1123 dir_ends_in_slash = '/' == _dbus_string_get_byte (dir,
1124 _dbus_string_get_length (dir) - 1);
1126 file_starts_with_slash = '/' == _dbus_string_get_byte (next_component, 0);
1128 if (dir_ends_in_slash && file_starts_with_slash)
1130 _dbus_string_shorten (dir, 1);
1132 else if (!(dir_ends_in_slash || file_starts_with_slash))
1134 if (!_dbus_string_append_byte (dir, '/'))
1138 return _dbus_string_copy (next_component, 0, dir,
1139 _dbus_string_get_length (dir));
1149 * Open a directory to iterate over.
1151 * @param filename the directory name
1152 * @param result return location for error code if #NULL returned
1153 * @returns new iterator, or #NULL on error
1156 _dbus_directory_open (const DBusString *filename,
1157 DBusResultCode *result)
1161 const char *filename_c;
1163 _dbus_string_get_const_data (filename, &filename_c);
1165 d = opendir (filename_c);
1168 dbus_set_result (result, _dbus_result_from_errno (errno));
1172 iter = dbus_new0 (DBusDirIter, 1);
1176 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1186 * Get next file in the directory. Will not return "." or ".."
1187 * on UNIX. If an error occurs, the contents of "filename"
1188 * are undefined. #DBUS_RESULT_SUCCESS is always returned
1189 * in result if no error occurs.
1191 * @todo for thread safety, I think we have to use
1192 * readdir_r(). (GLib has the same issue, should file a bug.)
1194 * @param iter the iterator
1195 * @param filename string to be set to the next file in the dir
1196 * @param result return location for error, or #DBUS_RESULT_SUCCESS
1197 * @returns #TRUE if filename was filled in with a new filename
1200 _dbus_directory_get_next_file (DBusDirIter *iter,
1201 DBusString *filename,
1202 DBusResultCode *result)
1204 /* we always have to put something in result, since return
1205 * value means whether there's a filename and doesn't
1206 * reliably indicate whether an error was set.
1210 dbus_set_result (result, DBUS_RESULT_SUCCESS);
1214 ent = readdir (iter->d);
1217 dbus_set_result (result,
1218 _dbus_result_from_errno (errno));
1221 else if (ent->d_name[0] == '.' &&
1222 (ent->d_name[1] == '\0' ||
1223 (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
1227 _dbus_string_set_length (filename, 0);
1228 if (!_dbus_string_append (filename, ent->d_name))
1230 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1239 * Closes a directory iteration.
1242 _dbus_directory_close (DBusDirIter *iter)
1249 /** @} end of sysdeps */