[dba5ed]: src / server.c Maximize Restore History

Download this file

server.c    293 lines (247 with data), 6.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Copyright (C) 2002 Stefan Holst
* Copyright (C) 2003-2006 the xine project team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* $Id: server.c,v 1.22 2006/03/27 22:11:25 dsalt Exp $
*
* gxine unix domain socket remote control interface
*/
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pthread.h>
#include <signal.h>
#include "globals.h"
#include "engine.h"
#include "server.h"
/*
#define LOG
*/
#define SOCKET_FILENAME "%s/.gxine/socket"
#define BUF_SIZE 1024
static int gxsocket;
static pthread_t server_thread;
/* create an unix domain socket */
static int make_socket (char *path)
{
struct sockaddr_un adr = {0};
int sock = socket (PF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
{
perror ("socket");
return -1;
}
unlink (path);
/* fill socket address structures */
adr.sun_family = AF_UNIX;
strncpy (adr.sun_path, path, sizeof (adr.sun_path)-1);
/* bind the name to the descriptor */
if (bind (sock, (struct sockaddr *) &adr, sizeof (adr)) < 0)
{
perror ("bind");
return -1;
}
logprintf ("server: socket '%s' created \n", path);
return sock;
}
static void sock_write (int fd, char *msg)
{
int len = strlen (msg);
int pos = 0;
while (pos < len)
{
int n = write (fd, msg+pos, len-pos);
if (n <= 0)
switch (errno) {
case EAGAIN:
continue;
default:
perror ("server: write error");
return;
}
else
pos += n;
}
}
static void __attribute__ ((format (printf, 2, 3)))
error_report (void *s, const char *msg, ...)
{
int fd = *(int *)s;
char *amsg;
va_list ap;
va_start (ap, msg);
amsg = g_strdup_vprintf (msg, ap);
va_end (ap);
sock_write (fd, amsg);
sock_write (fd, "\n\n");
free (amsg);
}
static int running;
static __attribute__ ((noreturn)) void *socket_listener (void *data)
{
listen (gxsocket, 1);
while (running)
{
struct sockaddr_un client;
socklen_t size;
char buf[BUF_SIZE + 1];
int s = accept (gxsocket, (struct sockaddr *) &client, &size);
if (s == -1)
{
perror ("accept");
sleep (1);
continue;
}
logprintf ("server: connected.\n");
sock_write(s, _("This is gxine\n\nJavascript interface. Type 'help();' for help.\n"));
int buf_len = 0;
int listening = 1;
while (listening)
{
int n = read (s, buf + buf_len, 1);
if (n <= 0)
{
switch (errno)
{
case EAGAIN:
continue;
default:
if (errno)
g_printerr (_("server: read error %d\n"), errno);
else
case ESPIPE:
case EPIPE:
case ECONNRESET:
g_print (_("server: client disconnected\n"));
listening = 0;
break;
}
}
else if (buf[buf_len] == '\n' || buf[buf_len] == '\r')
{
buf[buf_len]=0;
gdk_threads_enter();
engine_exec_ext (buf, error_report, &s, NULL, NULL);
gdk_threads_leave();
buf_len = 0;
}
else if (buf_len < BUF_SIZE)
buf_len += n;
}
logprintf ("server: closing client connection.\n");
close(s);
}
logprintf ("server: listener thread exit\n");
pthread_exit(NULL);
}
void server_setup (void)
{
char filename[FILENAME_MAX];
snprintf (filename, sizeof (filename), SOCKET_FILENAME, getenv ("HOME"));
gxsocket = make_socket (filename);
}
void server_start (void)
{
if (gxsocket >= 0)
{
pthread_attr_t attr;
running = 1;
pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, 64 * 1024); /* should be fine... */
pthread_create (&server_thread, &attr, socket_listener, NULL);
pthread_attr_destroy (&attr);
}
}
void server_stop (void)
{
if (running)
{
void *p;
running = 0;
sleep (1);
pthread_cancel (server_thread);
pthread_join (server_thread, &p);
/* shutdown (gxsocket, SHUT_RDWR);*/
close(gxsocket);
}
}
/*
* functions to connect to already running gxine
*/
static int client_fd = -1;
static char tstr[80];
static void sigpipe_handler (int sn)
{
logprintf ("server: SIGPIPE received.\n");
signal (SIGPIPE, sigpipe_handler);
}
int server_client_connect (void)
{
struct sockaddr_un cli_adr = {0}, serv_adr = {0};
int length = sizeof(struct sockaddr_un);
char filename[PATH_MAX];
signal (SIGPIPE, sigpipe_handler);
/* server filename */
snprintf (filename, PATH_MAX, SOCKET_FILENAME, getenv ("HOME"));
logprintf ("server: trying to connect to already running instance of gxine (%s)...\n",
filename);
client_fd = socket (AF_UNIX, SOCK_STREAM, 0);
/* initialize the client address structure */
cli_adr.sun_family = AF_UNIX;
sprintf (tstr, "/tmp/gxine_%d", getpid ());
strcpy (cli_adr.sun_path, tstr);
unlink (tstr);
/* bind the socket to the client's address */
if (bind (client_fd, (struct sockaddr*) &cli_adr, length) < 0)
{
perror ("bind");
return 0;
}
/* initialize the server address structure */
serv_adr.sun_family = AF_UNIX;
strcpy (serv_adr.sun_path, filename);
if (connect (client_fd, (struct sockaddr *) &serv_adr, sizeof (serv_adr)) < 0)
{
#ifdef LOG
perror ("connect");
#endif
server_client_stop ();
return 0;
}
g_print (_("server: connected to existing gxine instance.\n"));
atexit (server_client_stop);
return 1;
}
int server_client_send (const char *cmd)
{
return write (client_fd, cmd, strlen (cmd));
}
void server_client_stop (void)
{
if (client_fd < 0)
return;
close(client_fd);
client_fd = -1;
unlink (tstr);
}