./configure wasnt happy if gsf is present but no wv. Its happy now. pkg-check-modules...
[beagle.git] / libbeagle / beagle / beagle-util.c
blob5dd5ea4ae3089ff9e75b365a476fcef4e44fe112
1 /*
2 * beagle-util.c
4 * Copyright (C) 2005 Novell, Inc.
6 */
8 /*
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
36 #include "beagle-util.h"
38 GQuark
39 beagle_error_quark (void)
41 static GQuark quark;
43 if (!quark)
44 quark = g_quark_from_static_string ("BEAGLE_ERROR");
46 return quark;
49 gboolean
50 beagle_util_is_path_on_block_device (const char *path)
52 struct stat st;
54 if (stat (path, &st) < 0)
55 return FALSE;
57 return (st.st_dev >> 8 != 0);
60 char *
61 beagle_util_get_socket_path (const char *client_name)
63 const gchar *beagle_home;
64 gchar *socket_dir;
65 gchar *socket_path;
66 struct stat buf;
68 if (!client_name)
69 client_name = "socket";
71 beagle_home = g_getenv ("BEAGLE_HOME");
72 if (beagle_home == NULL)
73 beagle_home = g_get_home_dir ();
75 if (! beagle_util_is_path_on_block_device (beagle_home) ||
76 getenv ("BEAGLE_SYNCHRONIZE_LOCALLY") != NULL) {
77 gchar *remote_storage_dir = g_build_filename (beagle_home, ".beagle", "remote_storage_dir", NULL);
78 gchar *tmp;
80 if (! g_file_test (remote_storage_dir, G_FILE_TEST_EXISTS)) {
81 g_free (remote_storage_dir);
82 return NULL;
85 if (! g_file_get_contents (remote_storage_dir, &socket_dir, NULL, NULL)) {
86 g_free (remote_storage_dir);
87 return NULL;
90 g_free (remote_storage_dir);
92 /* There's a newline at the end that we want to strip off */
93 tmp = strrchr (socket_dir, '\n');
94 if (tmp != NULL)
95 *tmp = '\0';
97 if (! g_file_test (socket_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
98 g_free (socket_dir);
99 return NULL;
101 } else {
102 socket_dir = g_build_filename (beagle_home, ".beagle", NULL);
105 socket_path = g_build_filename (socket_dir, client_name, NULL);
106 g_free (socket_dir);
107 if (stat (socket_path, &buf) == -1 || !S_ISSOCK (buf.st_mode)) {
108 g_free (socket_path);
109 return NULL;
112 return socket_path;
116 gboolean
117 beagle_util_daemon_is_running (void)
119 gchar *socket_path;
120 int sockfd;
121 struct sockaddr_un sun;
123 socket_path = beagle_util_get_socket_path (NULL);
125 if (socket_path == NULL)
126 return FALSE;
128 bzero (&sun, sizeof (sun));
129 sun.sun_family = AF_UNIX;
130 snprintf (sun.sun_path, sizeof (sun.sun_path), socket_path);
132 g_free (socket_path);
134 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
135 if (sockfd < 0) {
136 return FALSE;
139 if (connect (sockfd, (struct sockaddr *) &sun, sizeof (sun)) < 0) {
140 return FALSE;
143 close (sockfd);
145 return TRUE;