annotate src/remote.c @ 637:8a1202efa51e

Add some wrapper functions for not yet common code.
author zas_
date Mon, 12 May 2008 00:22:38 +0000
parents f9bf33be53ff
children 3097880d7d95
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
507
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents: 506
diff changeset
17 #include "debug.h"
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
18
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
19 #include <sys/types.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
20 #include <sys/socket.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
21 #include <sys/un.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
22 #include <signal.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
23 #include <errno.h>
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
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
26 #define SERVER_MAX_CLIENTS 8
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
27
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
28 #define REMOTE_SERVER_BACKLOG 4
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
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
31 #ifndef UNIX_PATH_MAX
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
32 #define UNIX_PATH_MAX 108
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
33 #endif
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
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
36 typedef struct _RemoteClient RemoteClient;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
37 struct _RemoteClient {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
38 gint fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
39 gint channel_id;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
40 RemoteConnection *rc;
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
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
44 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
45 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
46 RemoteClient *client = data;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
47 RemoteConnection *rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
48
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
49 rc = client->rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
50
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
51 if (condition & G_IO_IN)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
52 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
53 GList *queue = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
54 GList *work;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
55 gchar *buffer = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
56 GError *error = NULL;
64
04ff0df3ad2f Mon Aug 15 17:13:57 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
57 guint termpos;
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
58
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
59 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
60 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
61 if (buffer)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
62 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
63 buffer[termpos] = '\0';
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
64
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
65 if (strlen(buffer) > 0)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
66 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
67 queue = g_list_append(queue, buffer);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
68 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
69 else
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
70 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
71 g_free(buffer);
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
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
74 buffer = NULL;
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
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
78 if (error)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
79 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
80 printf("error reading socket: %s\n", error->message);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
81 g_error_free(error);
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
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
84 work = queue;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
85 while (work)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
86 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
87 gchar *command = work->data;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
88 work = work->next;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
89
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
90 if (rc->read_func) rc->read_func(rc, command, rc->read_data);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
91 g_free(command);
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
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
94 g_list_free(queue);
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
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
97 if (condition & G_IO_HUP)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
98 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
99 rc->clients = g_list_remove(rc->clients, client);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
100
506
fc9c8a3e1a8b Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents: 495
diff changeset
101 DEBUG_1("HUP detected, closing client.");
fc9c8a3e1a8b Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents: 495
diff changeset
102 DEBUG_1("client count %d", g_list_length(rc->clients));
495
c7a2471e5c4e Introduce macros to display debug messages.
zas_
parents: 475
diff changeset
103
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
104 g_source_remove(client->channel_id);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
105 close(client->fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
106 g_free(client);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
107 }
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 return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
110 }
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 static void remote_server_client_add(RemoteConnection *rc, int fd)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
113 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
114 RemoteClient *client;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
115 GIOChannel *channel;
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 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
118 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
119 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
120 close(fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
121 return;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
122 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
123
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
124 client = g_new0(RemoteClient, 1);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
125 client->rc = rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
126 client->fd = fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
127
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
128 channel = g_io_channel_unix_new(fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
129 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
130 remote_server_client_cb, client, NULL);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
131 g_io_channel_unref(channel);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
132
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
133 rc->clients = g_list_append(rc->clients, client);
506
fc9c8a3e1a8b Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents: 495
diff changeset
134 DEBUG_1("client count %d", g_list_length(rc->clients));
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
135 }
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 static void remote_server_clients_close(RemoteConnection *rc)
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 while (rc->clients)
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 RemoteClient *client = rc->clients->data;
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 rc->clients = g_list_remove(rc->clients, client);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
144
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
145 g_source_remove(client->channel_id);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
146 close(client->fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
147 g_free(client);
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 }
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 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
152 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
153 RemoteConnection *rc = data;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
154 int fd;
64
04ff0df3ad2f Mon Aug 15 17:13:57 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
155 unsigned int alen;
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
156
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
157 fd = accept(rc->fd, NULL, &alen);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
158 if (fd == -1)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
159 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
160 printf("error accepting socket: %s\n", strerror(errno));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
161 return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
162 }
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 remote_server_client_add(rc, fd);
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 return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
167 }
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 static gint remote_server_exists(const gchar *path)
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 RemoteConnection *rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
172
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
173 /* verify server up */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
174 rc = remote_client_open(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
175 remote_close(rc);
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 if (rc) return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
178
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
179 /* unable to connect, remove socket file to free up address */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
180 unlink(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
181 return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
182 }
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 *remote_server_open(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
185 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
186 RemoteConnection *rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
187 struct sockaddr_un addr;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
188 gint sun_path_len;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
189 int fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
190 GIOChannel *channel;
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 if (remote_server_exists(path))
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
193 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
194 printf("Address already in use: %s\n", path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
195 return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
196 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
197
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
198 fd = socket(PF_UNIX, SOCK_STREAM, 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
199 if (fd == -1) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
200
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
201 addr.sun_family = AF_UNIX;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
202 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
203 strncpy(addr.sun_path, path, sun_path_len);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
204 if (bind(fd, &addr, sizeof(addr)) == -1 ||
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
205 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
206 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
207 printf("error subscribing to socket: %s\n", strerror(errno));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
208 close(fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
209 return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
210 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
211
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
212 rc = g_new0(RemoteConnection, 1);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
213 rc->server = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
214 rc->fd = fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
215 rc->path = g_strdup(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
216
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
217 rc->read_func = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
218 rc->read_data = 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 rc->clients = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
221
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
222 channel = g_io_channel_unix_new(rc->fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
223 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
224 remote_server_read_cb, rc, NULL);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
225 g_io_channel_unref(channel);
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 return rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
228 }
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 void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
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 if (!rc || !rc->server) return;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
233
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
234 rc->read_func = func;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
235 rc->read_data = data;
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
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 *remote_client_open(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
240 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
241 RemoteConnection *rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
242 struct stat st;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
243 struct sockaddr_un addr;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
244 gint sun_path_len;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
245 int fd;
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 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
248
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
249 fd = socket(PF_UNIX, SOCK_STREAM, 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
250 if (fd == -1) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
251
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
252 addr.sun_family = AF_UNIX;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
253 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
254 strncpy(addr.sun_path, path, sun_path_len);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
255 if (connect(fd, &addr, sizeof(addr)) == -1)
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 371
diff changeset
256 {
506
fc9c8a3e1a8b Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents: 495
diff changeset
257 DEBUG_1("error connecting to socket: %s", strerror(errno));
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 371
diff changeset
258 close(fd);
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 371
diff changeset
259 return NULL;
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 371
diff changeset
260 }
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
261
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
262 rc = g_new0(RemoteConnection, 1);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
263 rc->server = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
264 rc->fd = fd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
265 rc->path = g_strdup(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
266
371
7997b6704fdb this might fix the freezes on freebsd, solaris, etc.
nadvornik
parents: 281
diff changeset
267 /* this might fix the freezes on freebsd, solaris, etc. - completely untested */
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 371
diff changeset
268 remote_client_send(rc, "\n");
371
7997b6704fdb this might fix the freezes on freebsd, solaris, etc.
nadvornik
parents: 281
diff changeset
269
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
270 return rc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
271 }
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 sig_atomic_t sigpipe_occured = FALSE;
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 static void sighandler_sigpipe(int sig)
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 sigpipe_occured = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
278 }
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 gint remote_client_send(RemoteConnection *rc, const gchar *text)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
281 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
282 struct sigaction new_action, old_action;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
283 gint ret = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
284
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
285 if (!rc || rc->server) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
286 if (!text) return TRUE;
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 sigpipe_occured = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
289
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
290 new_action.sa_handler = sighandler_sigpipe;
512
f9bf33be53ff Remove whitespace between function name and first parenthesis for the sake of consistency.
zas_
parents: 507
diff changeset
291 sigemptyset(&new_action.sa_mask);
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
292 new_action.sa_flags = 0;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
293
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
294 /* setup our signal handler */
512
f9bf33be53ff Remove whitespace between function name and first parenthesis for the sake of consistency.
zas_
parents: 507
diff changeset
295 sigaction(SIGPIPE, &new_action, &old_action);
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
296
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
297 if (write(rc->fd, text, strlen(text)) == -1 ||
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
298 write(rc->fd, "\n", 1) == -1)
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 if (sigpipe_occured)
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 printf("SIGPIPE writing to socket: %s\n", rc->path);
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 else
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 printf("error writing to socket: %s\n", strerror(errno));
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 ret = FALSE;;
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 else
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 ret = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
313 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
314
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
315 /* restore the original signal handler */
512
f9bf33be53ff Remove whitespace between function name and first parenthesis for the sake of consistency.
zas_
parents: 507
diff changeset
316 sigaction(SIGPIPE, &old_action, NULL);
9
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 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
319 }
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 void remote_close(RemoteConnection *rc)
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) return;
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 if (rc->server)
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 remote_server_clients_close(rc);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
328
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
329 g_source_remove(rc->channel_id);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
330 unlink(rc->path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
331 }
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 close(rc->fd);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
334
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
335 g_free(rc->path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
336 g_free(rc);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
337 }