annotate src/remote.c @ 495:c7a2471e5c4e

Introduce macros to display debug messages. if (debug) printf(...) is now replaced by variadic macros: DEBUG_1() And for higher debugging levels: DEBUG_2() DEBUG_3() DEBUG_4()
author zas_
date Wed, 23 Apr 2008 20:47:19 +0000
parents 48c8e49b571c
children fc9c8a3e1a8b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
1 /*
196
f6e307c7bad6 rename GQview -> Geeqie over the code
nadvornik
parents: 64
diff changeset
2 * Geeqie
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
3 * (C) 2004 John Ellis
475
48c8e49b571c updated copyright in source files
nadvornik
parents: 446
diff changeset
4 * Copyright (C) 2008 The Geeqie Team
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
5 *
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
6 * Author: John Ellis
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
7 *
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
8 * This software is released under the GNU General Public License (GNU GPL).
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
9 * Please read the included file COPYING for more information.
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
10 * This software comes with no warranty of any kind, use at your own risk!
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
11 */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
12
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
13
281
9995c5fb202a gqview.h -> main.h
zas_
parents: 196
diff changeset
14 #include "main.h"
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
15 #include "remote.h"
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
16
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
17
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
18 #include <sys/types.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
19 #include <sys/socket.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
20 #include <sys/un.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
21 #include <signal.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
22 #include <errno.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
23
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
24
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
25 #define SERVER_MAX_CLIENTS 8
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
26
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
27 #define REMOTE_SERVER_BACKLOG 4
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
28
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
29
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
30 #ifndef UNIX_PATH_MAX
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
31 #define UNIX_PATH_MAX 108
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
32 #endif
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
33
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
34
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
35 typedef struct _RemoteClient RemoteClient;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
36 struct _RemoteClient {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
37 gint fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
38 gint channel_id;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
39 RemoteConnection *rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
40 };
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
41
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
42
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
43 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
44 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
45 RemoteClient *client = data;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
46 RemoteConnection *rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
47
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
48 rc = client->rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
49
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
50 if (condition & G_IO_IN)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
51 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
52 GList *queue = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
53 GList *work;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
54 gchar *buffer = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
55 GError *error = NULL;
64
04ff0df3ad2f Mon Aug 15 17:13:57 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
56 guint termpos;
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
57
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
58 while (g_io_channel_read_line(source, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
59 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
60 if (buffer)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
61 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
62 buffer[termpos] = '\0';
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
63
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
64 if (strlen(buffer) > 0)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
65 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
66 queue = g_list_append(queue, buffer);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
67 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
68 else
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
69 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
70 g_free(buffer);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
71 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
72
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
73 buffer = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
74 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
75 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
76
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
77 if (error)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
78 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
79 printf("error reading socket: %s\n", error->message);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
80 g_error_free(error);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
81 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
82
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
83 work = queue;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
84 while (work)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
85 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
86 gchar *command = work->data;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
87 work = work->next;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
88
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
89 if (rc->read_func) rc->read_func(rc, command, rc->read_data);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
90 g_free(command);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
91 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
92
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
93 g_list_free(queue);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
94 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
95
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
96 if (condition & G_IO_HUP)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
97 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
98 rc->clients = g_list_remove(rc->clients, client);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
99
495
c7a2471e5c4e Introduce macros to display debug messages.
zas_
parents: 475
diff changeset
100 DEBUG_1("HUP detected, closing client.\nclient count %d\n", g_list_length(rc->clients));
c7a2471e5c4e Introduce macros to display debug messages.
zas_
parents: 475
diff changeset
101
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
102 g_source_remove(client->channel_id);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
103 close(client->fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
104 g_free(client);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
105 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
106
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
107 return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
108 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
109
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
110 static void remote_server_client_add(RemoteConnection *rc, int fd)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
111 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
112 RemoteClient *client;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
113 GIOChannel *channel;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
114
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
115 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
116 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
117 printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
118 close(fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
119 return;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
120 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
121
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
122 client = g_new0(RemoteClient, 1);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
123 client->rc = rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
124 client->fd = fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
125
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
126 channel = g_io_channel_unix_new(fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
127 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
128 remote_server_client_cb, client, NULL);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
129 g_io_channel_unref(channel);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
130
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
131 rc->clients = g_list_append(rc->clients, client);
495
c7a2471e5c4e Introduce macros to display debug messages.
zas_
parents: 475
diff changeset
132 DEBUG_1("client count %d\n", g_list_length(rc->clients));
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
133 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
134
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
135 static void remote_server_clients_close(RemoteConnection *rc)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
136 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
137 while (rc->clients)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
138 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
139 RemoteClient *client = rc->clients->data;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
140
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
141 rc->clients = g_list_remove(rc->clients, client);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
142
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
143 g_source_remove(client->channel_id);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
144 close(client->fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
145 g_free(client);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
146 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
147 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
148
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
149 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
150 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
151 RemoteConnection *rc = data;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
152 int fd;
64
04ff0df3ad2f Mon Aug 15 17:13:57 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
153 unsigned int alen;
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
154
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
155 fd = accept(rc->fd, NULL, &alen);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
156 if (fd == -1)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
157 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
158 printf("error accepting socket: %s\n", strerror(errno));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
159 return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
160 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
161
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
162 remote_server_client_add(rc, fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
163
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
164 return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
165 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
166
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
167 static gint remote_server_exists(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
168 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
169 RemoteConnection *rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
170
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
171 /* verify server up */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
172 rc = remote_client_open(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
173 remote_close(rc);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
174
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
175 if (rc) return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
176
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
177 /* unable to connect, remove socket file to free up address */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
178 unlink(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
179 return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
180 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
181
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
182 RemoteConnection *remote_server_open(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
183 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
184 RemoteConnection *rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
185 struct sockaddr_un addr;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
186 gint sun_path_len;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
187 int fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
188 GIOChannel *channel;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
189
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
190 if (remote_server_exists(path))
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
191 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
192 printf("Address already in use: %s\n", path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
193 return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
194 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
195
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
196 fd = socket(PF_UNIX, SOCK_STREAM, 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
197 if (fd == -1) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
198
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
199 addr.sun_family = AF_UNIX;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
200 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
201 strncpy(addr.sun_path, path, sun_path_len);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
202 if (bind(fd, &addr, sizeof(addr)) == -1 ||
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
203 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
204 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
205 printf("error subscribing to socket: %s\n", strerror(errno));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
206 close(fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
207 return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
208 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
209
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
210 rc = g_new0(RemoteConnection, 1);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
211 rc->server = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
212 rc->fd = fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
213 rc->path = g_strdup(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
214
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
215 rc->read_func = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
216 rc->read_data = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
217
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
218 rc->clients = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
219
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
220 channel = g_io_channel_unix_new(rc->fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
221 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
222 remote_server_read_cb, rc, NULL);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
223 g_io_channel_unref(channel);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
224
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
225 return rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
226 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
227
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
228 void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
229 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
230 if (!rc || !rc->server) return;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
231
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
232 rc->read_func = func;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
233 rc->read_data = data;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
234 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
235
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
236
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
237 RemoteConnection *remote_client_open(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
238 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
239 RemoteConnection *rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
240 struct stat st;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
241 struct sockaddr_un addr;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
242 gint sun_path_len;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
243 int fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
244
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
245 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
246
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
247 fd = socket(PF_UNIX, SOCK_STREAM, 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
248 if (fd == -1) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
249
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
250 addr.sun_family = AF_UNIX;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
251 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
252 strncpy(addr.sun_path, path, sun_path_len);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
253 if (connect(fd, &addr, sizeof(addr)) == -1)
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 371
diff changeset
254 {
495
c7a2471e5c4e Introduce macros to display debug messages.
zas_
parents: 475
diff changeset
255 DEBUG_1("error connecting to socket: %s\n", strerror(errno));
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 371
diff changeset
256 close(fd);
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 371
diff changeset
257 return NULL;
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 371
diff changeset
258 }
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
259
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
260 rc = g_new0(RemoteConnection, 1);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
261 rc->server = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
262 rc->fd = fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
263 rc->path = g_strdup(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
264
371
7997b6704fdb this might fix the freezes on freebsd, solaris, etc.
nadvornik
parents: 281
diff changeset
265 /* this might fix the freezes on freebsd, solaris, etc. - completely untested */
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 371
diff changeset
266 remote_client_send(rc, "\n");
371
7997b6704fdb this might fix the freezes on freebsd, solaris, etc.
nadvornik
parents: 281
diff changeset
267
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
268 return rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
269 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
270
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
271 static sig_atomic_t sigpipe_occured = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
272
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
273 static void sighandler_sigpipe(int sig)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
274 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
275 sigpipe_occured = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
276 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
277
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
278 gint remote_client_send(RemoteConnection *rc, const gchar *text)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
279 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
280 struct sigaction new_action, old_action;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
281 gint ret = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
282
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
283 if (!rc || rc->server) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
284 if (!text) return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
285
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
286 sigpipe_occured = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
287
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
288 new_action.sa_handler = sighandler_sigpipe;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
289 sigemptyset (&new_action.sa_mask);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
290 new_action.sa_flags = 0;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
291
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
292 /* setup our signal handler */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
293 sigaction (SIGPIPE, &new_action, &old_action);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
294
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
295 if (write(rc->fd, text, strlen(text)) == -1 ||
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
296 write(rc->fd, "\n", 1) == -1)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
297 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
298 if (sigpipe_occured)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
299 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
300 printf("SIGPIPE writing to socket: %s\n", rc->path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
301 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
302 else
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
303 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
304 printf("error writing to socket: %s\n", strerror(errno));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
305 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
306 ret = FALSE;;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
307 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
308 else
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
309 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
310 ret = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
311 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
312
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
313 /* restore the original signal handler */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
314 sigaction (SIGPIPE, &old_action, NULL);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
315
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
316 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
317 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
318
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
319 void remote_close(RemoteConnection *rc)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
320 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
321 if (!rc) return;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
322
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
323 if (rc->server)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
324 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
325 remote_server_clients_close(rc);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
326
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
327 g_source_remove(rc->channel_id);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
328 unlink(rc->path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
329 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
330
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
331 close(rc->fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
332
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
333 g_free(rc->path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
334 g_free(rc);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
335 }