summaryrefslogtreecommitdiff
path: root/common/gvfsutils.c
blob: 47c31176c236d3a1ab22d659c009f551b68dd30e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright (C) 2014 Ross Lagerwall
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "gvfsutils.h"

#ifdef G_OS_UNIX
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif

/**
 * gvfs_randomize_string:
 * @str: the string to randomize
 * @len: the length of the string
 *
 * Takes a string and fills it with @len random chars.
 **/
void
gvfs_randomize_string (char *str,
                       int len)
{
  int i;
  const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

  for (i = 0; i < len; i++)
    str[i] = chars[g_random_int_range (0, strlen(chars))];
}

/**
 * gvfs_have_session_bus:
 *
 * Returns: %TRUE if we can connect to a session or user bus without
 *  triggering X11 autolaunching.
 */
gboolean
gvfs_have_session_bus (void)
{
  if (g_getenv ("DBUS_SESSION_BUS_ADDRESS") != NULL)
    return TRUE;

#ifdef G_OS_UNIX
    {
      gboolean ret = FALSE;
      gchar *bus;
      GStatBuf buf;

      bus = g_build_filename (g_get_user_runtime_dir (), "bus", NULL);

      if (g_stat (bus, &buf) < 0)
        goto out;

      if (buf.st_uid != geteuid ())
        goto out;

      if ((buf.st_mode & S_IFMT) != S_IFSOCK)
        goto out;

      ret = TRUE;
out:
      g_free (bus);
      return ret;
    }
#else
  return FALSE;
#endif
}