1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2014 Ross Lagerwall
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <config.h>
21 
22 #include <string.h>
23 #include <glib.h>
24 #include <glib/gstdio.h>
25 #include <signal.h>
26 #include "gvfsutils.h"
27 
28 #ifdef G_OS_UNIX
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #endif
33 
34 /* Indicates whether debug output is enabled. */
35 static gboolean debugging = FALSE;
36 
37 /**
38  * gvfs_randomize_string:
39  * @str: the string to randomize
40  * @len: the length of the string
41  *
42  * Takes a string and fills it with @len random chars.
43  **/
44 void
gvfs_randomize_string(char * str,int len)45 gvfs_randomize_string (char *str,
46                        int len)
47 {
48   int i;
49   const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
50 
51   for (i = 0; i < len; i++)
52     str[i] = chars[g_random_int_range (0, strlen(chars))];
53 }
54 
55 /**
56  * gvfs_have_session_bus:
57  *
58  * Returns: %TRUE if we can connect to a session or user bus without
59  *  triggering X11 autolaunching.
60  */
61 gboolean
gvfs_have_session_bus(void)62 gvfs_have_session_bus (void)
63 {
64   if (g_getenv ("DBUS_SESSION_BUS_ADDRESS") != NULL)
65     return TRUE;
66 
67 #ifdef G_OS_UNIX
68     {
69       gboolean ret = FALSE;
70       gchar *bus;
71       GStatBuf buf;
72 
73       bus = g_build_filename (g_get_user_runtime_dir (), "bus", NULL);
74 
75       if (g_stat (bus, &buf) < 0)
76         goto out;
77 
78       if (buf.st_uid != geteuid ())
79         goto out;
80 
81       if ((buf.st_mode & S_IFMT) != S_IFSOCK)
82         goto out;
83 
84       ret = TRUE;
85 out:
86       g_free (bus);
87       return ret;
88     }
89 #else
90   return FALSE;
91 #endif
92 }
93 
94 gboolean
gvfs_get_debug(void)95 gvfs_get_debug (void)
96 {
97   return debugging;
98 }
99 
100 void
gvfs_set_debug(gboolean debugging_)101 gvfs_set_debug (gboolean debugging_)
102 {
103   debugging = debugging_;
104 }
105 
106 static void
toggle_debugging(int signum)107 toggle_debugging (int signum)
108 {
109   debugging = !debugging;
110 }
111 
112 /**
113  * gvfs_setup_debugging_handler:
114  *
115  * Sets up a handler for SIGUSR2 that toggles the debugging flag when the
116  * signal is received.
117  **/
118 void
gvfs_setup_debug_handler(void)119 gvfs_setup_debug_handler (void)
120 {
121   struct sigaction sa;
122 
123   sigemptyset (&sa.sa_mask);
124   sa.sa_handler = toggle_debugging;
125   sa.sa_flags = 0;
126   sigaction (SIGUSR2, &sa, NULL);
127 }
128 
129 gboolean
gvfs_is_ipv6(const char * host)130 gvfs_is_ipv6 (const char *host)
131 {
132   g_return_val_if_fail (host != NULL, FALSE);
133 
134   if (*host != '[' || host[strlen (host) - 1] != ']')
135     return FALSE;
136 
137   return TRUE;
138 }
139