File: clientserver.c

package info (click to toggle)
gtkpod 0.99.14-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,716 kB
  • ctags: 3,266
  • sloc: ansic: 43,120; sh: 4,837; xml: 1,316; lex: 637; perl: 264; makefile: 236; awk: 73; python: 35
file content (325 lines) | stat: -rw-r--r-- 7,916 bytes parent folder | download
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
|  Copyright (C) 2002-2005 Jorg Schuler <jcsjcs at users sourceforge net>
|  Part of the gtkpod project.
| 
|  URL: http://www.gtkpod.org/
|  URL: http://gtkpod.sourceforge.net/
|
|  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|  iTunes and iPod are trademarks of Apple
|
|  This product is not supported/written/published by Apple!
|
|  $Id: clientserver.c 1852 2007-12-20 15:25:48Z Sikon $
*/

#include "config.h"
#include "clientserver.h"
#include "sha1.h"
#include "misc.h"
#include "display_itdb.h"
#include "prefs.h"

#include <errno.h>
#include <fcntl.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>

static gint ssock=-1;
static struct sockaddr_un *saddr = NULL;
static guint inp_handler;

const gchar *SOCKET_TEST="TEST:";
const gchar *SOCKET_PLYC="PLYC:";


#ifndef HAVE_FLOCK
/* emulate flock on systems that do not have it */
int flock(int fd, int operation)
{
    struct flock      f;
    memset(&f, 0, sizeof (f));

    switch (operation)
    {
    case LOCK_EX:
	f.l_type = F_WRLCK;
	return fcntl(fd, F_SETLKW, &f);
    default:
	g_warning ("*** flock operation '%d' not implemented.\n",
		   operation);
	return -1;
    }
}
#endif


/* set the path to the socket name */
static void set_path (struct sockaddr_un *saddr)
{
    snprintf (saddr->sun_path, sizeof (saddr->sun_path), "%s%sgtkpod-%s",
	      g_get_tmp_dir(), G_DIR_SEPARATOR_S, g_get_user_name());
}


/* checks if the socket "/tmp/gtkpod-<user>" is already being used.
   Return value:
   TRUE: socket already being used
   FALSE: socket not being used */
static gboolean socket_used ()
{
    struct sockaddr_un *server;
    gboolean result = FALSE;

    server = g_malloc0 (sizeof (struct sockaddr_un));
    set_path (server);
    if (g_file_test (server->sun_path, G_FILE_TEST_EXISTS))
    {
	gint csock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (csock != -1)
	{
	    server->sun_family = AF_UNIX;
	    if (connect(csock, (struct sockaddr *)server,
			sizeof(struct sockaddr_un)) != -1)
	    {
		size_t socket_test_len = strlen(SOCKET_TEST);
		if (write(csock, SOCKET_TEST, socket_test_len) == 
		    (ssize_t) socket_test_len)
		    result = TRUE;
	    }
	    close(csock);
	}
    }
    g_free (server);
    return result;
}


/* append the filename <file> to ~/.gtkpod/offline_playcount */
static gboolean register_playcount (gchar *file)
{
    if (file && *file)
    {
	gchar *cfgdir = prefs_get_cfgdir ();

	if (cfgdir)
	{
	    gchar *offlplyc = g_strdup_printf (
		"%s%c%s", cfgdir, G_DIR_SEPARATOR, "offline_playcount");
	    int fd = open (offlplyc, O_WRONLY|O_CREAT|O_APPEND,
			   S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
	    if (fd != -1)
	    {
		if (flock (fd, LOCK_EX) == 0)
		{
		    gchar *sha1 = sha1_hash_on_filename (file, TRUE);
		    write (fd, SOCKET_PLYC, strlen (SOCKET_PLYC));
		    if (sha1) write (fd, sha1, strlen (sha1));
		    write (fd, " ", 1);
		    write (fd, file, strlen (file));
		    write (fd, "\n", 1);
		    g_free (sha1);
		}
		else
		{
		    fprintf (stderr, "couldn't lock %s\n", file);
		}
		close (fd);
	    }
	    else
	    {
		fprintf (stderr, "couldn't open %s\n", file);
	    }
	    g_free (offlplyc);
	    g_free (cfgdir);
	}
    }
    return TRUE;
}


void received_message (gpointer data, gint source, GdkInputCondition condition)
{
    gint csock, rval;
    gchar *buf;
/*    printf("received message\n");*/

    buf = g_malloc (PATH_MAX);
    while ((csock = accept(source, 0, 0)) != -1)
    {
	do
	{
	    bzero(buf, PATH_MAX);
	    if ((rval = read(csock, buf, PATH_MAX)) < 0)
	    {
		fprintf(stderr,"server: read error: %s",strerror(errno));
		continue;
	    }
	    else if (rval == 0)
	    {
		/* forget about it */
	    }
	    else
	    {
		if (strncmp (buf, SOCKET_TEST, strlen (SOCKET_TEST)) == 0)
		{
		    continue;  /* skip socket tests */
		}
		if (strncmp (buf, SOCKET_PLYC, strlen (SOCKET_PLYC)) == 0)
		{
		    gchar *file = buf + strlen (SOCKET_PLYC);
		    if (gp_increase_playcount (NULL, file, 1) == FALSE)
		    {   /* didn't find the track --> write to
			   offline_playcount */
			register_playcount (file);
		    }
		}
	    }
	}
	while (rval > 0);
	close(csock);
    }
    g_free (buf);
}


void server_setup (void)
{
    if (ssock != -1) return;  /* already opened */
    if (socket_used ())
    {  /* we are not the first instance of gtkpod -- the socket is
	  already being used, so we pass */
	gtkpod_warning (_("Another instance of gtkpod was detected. Playcount server not started.\n"));
	return;
    }
    ssock = socket(AF_UNIX, SOCK_STREAM, 0);
    if (ssock != -1)
    {
	if (saddr == NULL)
	{
	    saddr = g_malloc0 (sizeof (struct sockaddr_un));
	    saddr->sun_family = AF_UNIX;
	}
	set_path (saddr);
	unlink (saddr->sun_path);
	if (bind(ssock, (struct sockaddr *)saddr,
		 sizeof(struct sockaddr_un)) != -1)
	{
	    listen(ssock, 5);
	    /* socket must be non-blocking -- otherwise
	       received_message() will block */
	    fcntl (ssock, F_SETFL, O_NONBLOCK);
	    inp_handler = gtk_input_add_full (ssock, GDK_INPUT_READ,
					      received_message, NULL,
					      NULL, NULL);
	}
	else
	{
	    fprintf(stderr,"server: bind error: %s",strerror(errno));
	    close (ssock);
	    ssock = -1;
	}
    }
    else
    {
	fprintf(stderr,"server: socket error: %s",strerror(errno));
    }
}


void server_shutdown (void)
{
    if (ssock != -1)
    {
	gtk_input_remove (inp_handler);
	close (ssock);
	ssock = -1;
    }
    if (saddr)
    {
	if (strlen (saddr->sun_path) != 0)  unlink (saddr->sun_path);
	g_free (saddr);
	saddr = 0;
    }
}

/* Increment the playcount of <file> by one. Either connect to a
   running instance of gtkpod and transfer the filename, or write the
   name to ~/.gtkpod/offline_playcounts.
   Return value: TRUE on success, FALSE if a non-recoverable error
   occurred */
gboolean client_playcount (gchar *file)
{
    if (socket_used ())
    {   /* send filename to currently running gtkpod instance */
	struct sockaddr_un *server;

	server = g_malloc0 (sizeof (struct sockaddr_un));
	set_path (server);
	if (g_file_test (server->sun_path, G_FILE_TEST_EXISTS))
	{
	    gint csock = socket(AF_UNIX, SOCK_STREAM, 0);
	    if (csock != -1)
	    {
		server->sun_family = AF_UNIX;
		if (connect(csock, (struct sockaddr *)server,
			    sizeof(struct sockaddr_un)) != -1)
		{
		    gchar *buf = g_strdup_printf ("%s%s",
						  SOCKET_PLYC, file);
		    size_t buf_len = strlen(buf);
		    if (write(csock, buf, buf_len) != (ssize_t) buf_len)
		    {
			fprintf (stderr, "Error communicating to server. Playcount registered in offline database.\n");
			register_playcount (file);
		    }
		    g_free (buf);
		}
		close(csock);
	    }
	}
	g_free (server);
    }
    else
    {   /* write filename to ~/.gtkpod/offline_playcounts */
	register_playcount (file);
    }
    return TRUE;
}


/* print out the "sha1" hash of a filename <file> */
gboolean print_sha1_hash (gchar *file)
{
    if (file && *file)
    {
	gchar *hash = sha1_hash_on_filename (file, TRUE);
	if (hash) fprintf (stdout, "%s\n", hash);
	g_free (hash);
    }
    return TRUE;
}