4 * Copyright (C) 2005 Novell, Inc.
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.
30 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include "beagle-util.h"
39 beagle_error_quark (void)
44 quark
= g_quark_from_static_string ("BEAGLE_ERROR");
50 beagle_util_is_path_on_block_device (const char *path
)
54 if (stat (path
, &st
) < 0)
57 return (st
.st_dev
>> 8 != 0);
61 beagle_util_get_socket_path (const char *client_name
)
63 const gchar
*beagle_home
;
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
);
80 if (! g_file_test (remote_storage_dir
, G_FILE_TEST_EXISTS
)) {
81 g_free (remote_storage_dir
);
85 if (! g_file_get_contents (remote_storage_dir
, &socket_dir
, NULL
, NULL
)) {
86 g_free (remote_storage_dir
);
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');
97 if (! g_file_test (socket_dir
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
102 socket_dir
= g_build_filename (beagle_home
, ".beagle", NULL
);
105 socket_path
= g_build_filename (socket_dir
, client_name
, NULL
);
107 if (stat (socket_path
, &buf
) == -1 || !S_ISSOCK (buf
.st_mode
)) {
108 g_free (socket_path
);
117 beagle_util_daemon_is_running (void)
121 struct sockaddr_un sun
;
123 socket_path
= beagle_util_get_socket_path (NULL
);
125 if (socket_path
== NULL
)
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);
139 if (connect (sockfd
, (struct sockaddr
*) &sun
, sizeof (sun
)) < 0) {