Merge "Optional autogen.sh flag --enable-kdbus-transport added allowing to compile...
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-util-unix.c
index ba8ca7f..631c199 100644 (file)
 #include <sys/socket.h>
 #include <dirent.h>
 #include <sys/un.h>
+
+#ifdef HAVE_SYSLOG_H
 #include <syslog.h>
+#endif
 
 #ifdef HAVE_SYS_SYSLIMITS_H
 #include <sys/syslimits.h>
 #endif
 
+#include "sd-daemon.h"
+
 #ifndef O_BINARY
 #define O_BINARY 0
 #endif
@@ -123,6 +128,7 @@ _dbus_become_daemon (const DBusString *pidfile,
             dup2 (dev_null_fd, 2);
           else
             _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
+          close (dev_null_fd);
         }
 
       if (!keep_umask)
@@ -254,8 +260,8 @@ _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
       DBusString pid;
       int bytes;
 
-      _dbus_verbose ("writing our pid to pipe %"PRIuPTR"\n",
-                     print_pid_pipe->fd_or_handle);
+      _dbus_verbose ("writing our pid to pipe %d\n",
+                     print_pid_pipe->fd);
       
       if (!_dbus_string_init (&pid))
         {
@@ -375,13 +381,13 @@ _dbus_change_to_daemon_user  (const char    *user,
 
 /**
  * Attempt to ensure that the current process can open
- * at least @limit file descriptors.
+ * at least @limit file descriptors.
  *
- * If @limit is lower than the current, it will not be
+ * If @limit is lower than the current, it will not be
  * lowered.  No error is returned if the request can
  * not be satisfied.
  *
- * @limit Number of file descriptors
+ * @param limit number of file descriptors
  */
 void
 _dbus_request_file_descriptor_limit (unsigned int limit)
@@ -389,7 +395,6 @@ _dbus_request_file_descriptor_limit (unsigned int limit)
 #ifdef HAVE_SETRLIMIT
   struct rlimit lim;
   struct rlimit target_lim;
-  unsigned int current_limit;
 
   /* No point to doing this practically speaking
    * if we're not uid 0.  We expect the system
@@ -422,18 +427,28 @@ _dbus_request_file_descriptor_limit (unsigned int limit)
 #endif
 }
 
-void 
-_dbus_init_system_log (void)
+void
+_dbus_init_system_log (dbus_bool_t is_daemon)
 {
-  openlog ("dbus", LOG_PID, LOG_DAEMON);
+#ifdef HAVE_SYSLOG_H
+  int logopts = LOG_PID;
+
+#if HAVE_DECL_LOG_PERROR
+#ifdef HAVE_SYSTEMD
+  if (!is_daemon || sd_booted () <= 0)
+#endif
+    logopts |= LOG_PERROR;
+#endif
+
+  openlog ("dbus", logopts, LOG_DAEMON);
+#endif
 }
+
 /**
  * Log a message to the system log file (e.g. syslog on Unix).
  *
  * @param severity a severity value
  * @param msg a printf-style format string
- * @param args arguments for the format string
- *
  */
 void
 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
@@ -460,6 +475,8 @@ _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
 void
 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
 {
+  va_list tmp;
+#ifdef HAVE_SYSLOG_H
   int flags;
   switch (severity)
     {
@@ -476,7 +493,21 @@ _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args
         return;
     }
 
-  vsyslog (flags, msg, args);
+  DBUS_VA_COPY (tmp, args);
+  vsyslog (flags, msg, tmp);
+  va_end (tmp);
+#endif
+
+#if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR
+    {
+      /* vsyslog() won't write to stderr, so we'd better do it */
+      DBUS_VA_COPY (tmp, args);
+      fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
+      vfprintf (stderr, msg, tmp);
+      fputc ('\n', stderr);
+      va_end (tmp);
+    }
+#endif
 
   if (severity == DBUS_SYSTEM_LOG_FATAL)
     exit (1);
@@ -523,7 +554,7 @@ _dbus_user_at_console (const char *username,
                        DBusError  *error)
 {
 
-  DBusString f;
+  DBusString u, f;
   dbus_bool_t result;
 
   result = FALSE;
@@ -539,8 +570,9 @@ _dbus_user_at_console (const char *username,
       goto out;
     }
 
+  _dbus_string_init_const (&u, username);
 
-  if (!_dbus_string_append (&f, username))
+  if (!_dbus_concat_dir_and_file (&f, &u))
     {
       _DBUS_SET_OOM (error);
       goto out;
@@ -1146,3 +1178,180 @@ fail:
   _dbus_string_free (&path);
   return FALSE;
 }
+
+/*
+ * replaces the term DBUS_PREFIX in configure_time_path by the
+ * current dbus installation directory. On unix this function is a noop
+ *
+ * @param configure_time_path
+ * @return real path
+ */
+const char *
+_dbus_replace_install_prefix (const char *configure_time_path)
+{
+  return configure_time_path;
+}
+
+#define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
+#define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services"
+
+/**
+ * Returns the standard directories for a session bus to look for service
+ * activation files
+ *
+ * On UNIX this should be the standard xdg freedesktop.org data directories:
+ *
+ * XDG_DATA_HOME=${XDG_DATA_HOME-$HOME/.local/share}
+ * XDG_DATA_DIRS=${XDG_DATA_DIRS-/usr/local/share:/usr/share}
+ *
+ * and
+ *
+ * DBUS_DATADIR
+ *
+ * @param dirs the directory list we are returning
+ * @returns #FALSE on OOM
+ */
+
+dbus_bool_t
+_dbus_get_standard_session_servicedirs (DBusList **dirs)
+{
+  const char *xdg_data_home;
+  const char *xdg_data_dirs;
+  DBusString servicedir_path;
+
+  if (!_dbus_string_init (&servicedir_path))
+    return FALSE;
+
+  xdg_data_home = _dbus_getenv ("XDG_DATA_HOME");
+  xdg_data_dirs = _dbus_getenv ("XDG_DATA_DIRS");
+
+  if (xdg_data_home != NULL)
+    {
+      if (!_dbus_string_append (&servicedir_path, xdg_data_home))
+        goto oom;
+    }
+  else
+    {
+      const DBusString *homedir;
+      DBusString local_share;
+
+      if (!_dbus_homedir_from_current_process (&homedir))
+        goto oom;
+
+      if (!_dbus_string_append (&servicedir_path, _dbus_string_get_const_data (homedir)))
+        goto oom;
+
+      _dbus_string_init_const (&local_share, "/.local/share");
+      if (!_dbus_concat_dir_and_file (&servicedir_path, &local_share))
+        goto oom;
+    }
+
+  if (!_dbus_string_append (&servicedir_path, ":"))
+    goto oom;
+
+  if (xdg_data_dirs != NULL)
+    {
+      if (!_dbus_string_append (&servicedir_path, xdg_data_dirs))
+        goto oom;
+
+      if (!_dbus_string_append (&servicedir_path, ":"))
+        goto oom;
+    }
+  else
+    {
+      if (!_dbus_string_append (&servicedir_path, "/usr/local/share:/usr/share:"))
+        goto oom;
+    }
+
+  /*
+   * add configured datadir to defaults
+   * this may be the same as an xdg dir
+   * however the config parser should take
+   * care of duplicates
+   */
+  if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR))
+    goto oom;
+
+  if (!_dbus_split_paths_and_append (&servicedir_path,
+                                     DBUS_UNIX_STANDARD_SESSION_SERVICEDIR,
+                                     dirs))
+    goto oom;
+
+  _dbus_string_free (&servicedir_path);
+  return TRUE;
+
+ oom:
+  _dbus_string_free (&servicedir_path);
+  return FALSE;
+}
+
+
+/**
+ * Returns the standard directories for a system bus to look for service
+ * activation files
+ *
+ * On UNIX this should be the standard xdg freedesktop.org data directories:
+ *
+ * XDG_DATA_DIRS=${XDG_DATA_DIRS-/usr/local/share:/usr/share}
+ *
+ * and
+ *
+ * DBUS_DATADIR
+ *
+ * On Windows there is no system bus and this function can return nothing.
+ *
+ * @param dirs the directory list we are returning
+ * @returns #FALSE on OOM
+ */
+
+dbus_bool_t
+_dbus_get_standard_system_servicedirs (DBusList **dirs)
+{
+  /*
+   * DBUS_DATADIR may be the same as one of the standard directories. However,
+   * the config parser should take care of the duplicates.
+   *
+   * Also, append /lib as counterpart of /usr/share on the root
+   * directory (the root directory does not know /share), in order to
+   * facilitate early boot system bus activation where /usr might not
+   * be available.
+   */
+  static const char standard_search_path[] =
+    "/usr/local/share:"
+    "/usr/share:"
+    DBUS_DATADIR ":"
+    "/lib";
+  DBusString servicedir_path;
+
+  _dbus_string_init_const (&servicedir_path, standard_search_path);
+
+  return _dbus_split_paths_and_append (&servicedir_path,
+                                       DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR,
+                                       dirs);
+}
+
+/**
+ * Append the absolute path of the system.conf file
+ * (there is no system bus on Windows so this can just
+ * return FALSE and print a warning or something)
+ *
+ * @param str the string to append to
+ * @returns #FALSE if no memory
+ */
+dbus_bool_t
+_dbus_append_system_config_file (DBusString *str)
+{
+  return _dbus_string_append (str, DBUS_SYSTEM_CONFIG_FILE);
+}
+
+/**
+ * Append the absolute path of the session.conf file.
+ *
+ * @param str the string to append to
+ * @returns #FALSE if no memory
+ */
+dbus_bool_t
+_dbus_append_session_config_file (DBusString *str)
+{
+  return _dbus_string_append (str, DBUS_SESSION_CONFIG_FILE);
+}