1 /*
2  *   LASH
3  *
4  *   Copyright (C) 2002, 2003 Robert Ham <rah@bash.sh>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include <stdlib.h>
22 #include <string.h>
23 #include <strings.h>
24 #include <pthread.h>
25 #include <sys/types.h>
26 #include <sys/time.h>
27 #include <sys/resource.h>
28 #include <sys/wait.h>
29 #include <sys/socket.h>
30 #include <netdb.h>
31 extern int h_errno;
32 
33 #include <errno.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <unistd.h>
37 #include <sys/param.h>
38 
39 #include <lash/lash.h>
40 #include <lash/internal_headers.h>
41 
42 lash_args_t *
lash_extract_args(int * argc,char *** argv)43 lash_extract_args(int *argc, char ***argv)
44 {
45 	int i, j, valid_count;
46 	lash_args_t *args;
47 
48 	args = lash_args_new();
49 
50 	for (i = 1; i < *argc; ++i) {
51 		if (strncasecmp("--lash-server=", (*argv)[i], 14) == 0) {
52 			LASH_DEBUGARGS("setting server from command line: '%s'",
53 						   (*argv)[i] + 14);
54 			lash_args_set_server(args, (*argv)[i] + 14);
55 			(*argv)[i] = NULL;
56 			continue;
57 		}
58 
59 		if (strncasecmp("--lash-project=", (*argv)[i], 15) == 0) {
60 			LASH_DEBUGARGS("setting project from command line: '%s'",
61 						   (*argv)[i] + 15);
62 			lash_args_set_project(args, (*argv)[i] + 15);
63 			(*argv)[i] = NULL;
64 			continue;
65 		}
66 
67 		if (strncmp("--lash-id=", (*argv)[i], 10) == 0) {
68 			uuid_t id;
69 
70 			LASH_DEBUGARGS("setting id from command line: '%s'",
71 						   (*argv)[i] + 10);
72 
73 			j = uuid_parse((*argv)[i] + 10, id);
74 
75 			LASH_PRINT_DEBUG("id parsed");
76 
77 			(*argv)[i] = NULL;
78 
79 			if (j == -1) {
80 				fprintf(stderr,
81 						"%s: ERROR PARSING ID FROM COMMAND LINE!  THIS IS BAD!\n",
82 						__FUNCTION__);
83 			} else {
84 				lash_args_set_id(args, id);
85 			}
86 			continue;
87 		}
88 
89 		if (strncmp("--lash-no-autoresume", (*argv)[i], 20) == 0) {
90 			LASH_PRINT_DEBUG
91 				("setting LASH_No_Autoresume flag from command line");
92 
93 			lash_args_set_flag(args, LASH_No_Autoresume);
94 
95 			(*argv)[i] = NULL;
96 
97 			continue;
98 		}
99 
100 		if (strncmp("--lash-no-start-server", (*argv)[i], 22) == 0) {
101 			LASH_PRINT_DEBUG
102 				("setting LASH_No_Start_Server flag from command line");
103 
104 			lash_args_set_flag(args, LASH_No_Start_Server);
105 
106 			(*argv)[i] = NULL;
107 
108 			continue;
109 		}
110 	}
111 
112 	LASH_PRINT_DEBUG("args checked");
113 
114 	/* sort out the argv pointers */
115 	valid_count = *argc;
116 	for (i = 1; i < valid_count; ++i) {
117 		LASH_DEBUGARGS("checking argv[%d]", i);
118 		if ((*argv)[i] == NULL) {
119 
120 			for (j = i; j < *argc - 1; ++j)
121 				(*argv)[j] = (*argv)[j + 1];
122 
123 			valid_count--;
124 			i--;
125 		}
126 	}
127 
128 	LASH_PRINT_DEBUG("done");
129 
130 	*argc = valid_count;
131 
132 	lash_args_set_args(args, *argc, (const char**)*argv);
133 
134 	return args;
135 }
136 
137 lash_client_t *
lash_init(const lash_args_t * args,const char * class,int client_flags,lash_protocol_t protocol)138 lash_init(const lash_args_t * args,
139 		  const char *class, int client_flags, lash_protocol_t protocol)
140 {
141 	lash_connect_params_t *connect_params;
142 	lash_client_t *client;
143 	int err;
144 	char *str;
145 	const char *cstr;
146 	char wd[MAXPATHLEN];
147 	int tries;
148 	uuid_t id;
149 
150 	client = lash_client_new();
151 	connect_params = lash_connect_params_new();
152 
153 	client->args = lash_args_duplicate(args);
154 	client->args->flags |= client_flags;
155 	lash_client_set_class(client, class);
156 
157 	str = getcwd(wd, MAXPATHLEN);
158 	if (!str) {
159 		fprintf(stderr, "%s: could not get current working directory: %s\n",
160 				__FUNCTION__, strerror(errno));
161 		str = getenv("PWD");
162 		if (str)
163 			lash_connect_params_set_working_dir(connect_params, str);
164 		else
165 			lash_connect_params_set_working_dir(connect_params,
166 												getenv("HOME"));
167 	} else {
168 		lash_connect_params_set_working_dir(connect_params, str);
169 	}
170 
171 	LASH_DEBUGARGS("protocol version for connect: %s",
172 				   lash_protocol_string(protocol));
173 	connect_params->protocol_version = protocol;
174 	connect_params->flags = client->args->flags;
175 	lash_connect_params_set_project(connect_params, args->project);
176 	lash_connect_params_set_class(connect_params, class);
177 	uuid_copy(connect_params->id, args->id);
178 	connect_params->argc = args->argc;
179 	connect_params->argv = args->argv;
180 
181 	/* try and connect to the server */
182 	LASH_PRINT_DEBUG("connecting to server");
183 	cstr = lash_args_get_server(args);
184 	err = lash_comm_connect_to_server(client,
185 									  cstr ? cstr : "localhost",
186 									  LASH_PORT, connect_params);
187 
188 	/* couldn't connect, try to start a new server */
189 	/* but not if this client has been started by a server, in which
190 	   case something must be broken if we can't connect */
191 	if ( !(client_flags & LASH_No_Start_Server) ) {
192 		lash_args_get_id(args, id);
193 		if (err && getenv("LASH_NO_START_SERVER") == NULL && uuid_is_null(id)) {
194 			LASH_DEBUGARGS("%s: trying to start new LASH server\n",
195 					__FUNCTION__);
196 
197 			/* using the same double fork() trick as JACK does to prevent
198 			   zombie children */
199 			err = fork();
200 
201 			/* child process will run this statement */
202 			if (err == 0) {
203 
204 				/* need to close all open file descriptors except the std ones */
205 				struct rlimit max_fds;
206 				rlim_t fd;
207 
208 				getrlimit(RLIMIT_NOFILE, &max_fds);
209 
210 				for (fd = 3; fd < max_fds.rlim_cur; ++fd)
211 					close(fd);
212 
213 				switch (fork()) {
214 
215 					/* grandchild process will run this block */
216 					case 0:
217 						setsid();
218 						execlp("lashd", "lashd", NULL);
219 						_exit(-1);
220 
221 					/* this block only runs if the second fork() fails */
222 					case -1:
223 						_exit (-1);
224 
225 					/* exit the child process here */
226 					default:
227 						_exit (0);
228 				}
229 			}
230 
231 			/* if the fork succeeded, try to connect to the new server */
232 			else if (err > 0) {
233 				waitpid(err, NULL, 0);
234 				for (tries = 0; tries < 5; ++tries) {
235 					sleep(1);
236 					err = lash_comm_connect_to_server(client,
237 							cstr ? cstr : "localhost",
238 							LASH_PORT, connect_params);
239 					if (err == 0) {
240 						LASH_PRINT_DEBUG("successfully launched and connected to lashd");
241 						break;
242 					}
243 				}
244 				/* fork failed */
245 			} else {
246 				fprintf(stderr, "%s: fork failed while starting new server: %s\n",
247 						__FUNCTION__, strerror(err));
248 			}
249 		} else {
250 			fprintf(stderr, "%s: Not attempting to start daemon server automatically\n",
251 					__FUNCTION__);
252 		}
253 
254 	}
255 
256 	// this deletes the contained strings, but we don't want to do that
257 	// since they point to the strings in args
258 	//lash_connect_params_destroy(connect_params);
259 	connect_params = NULL;
260 
261 	if (err) {
262 		fprintf(stderr,
263 				"%s: could not connect to server '%s' - disabling LASH\n",
264 				__FUNCTION__, cstr ? cstr : "localhost");
265 
266 		lash_client_destroy(client);
267 		return NULL;
268 	}
269 	LASH_PRINT_DEBUG("connected to server");
270 	client->server_connected = 1;
271 
272 	err =
273 		pthread_create(&client->recv_thread, NULL, lash_comm_recv_run,
274 					   client);
275 	if (err) {
276 		fprintf(stderr,
277 				"%s: error creating recieve thread - disabling LASH: %s\n",
278 				__FUNCTION__, strerror(err));
279 
280 		lash_client_destroy(client);
281 		return NULL;
282 	}
283 
284 	err =
285 		pthread_create(&client->send_thread, NULL, lash_comm_send_run,
286 					   client);
287 	if (err) {
288 		fprintf(stderr,
289 				"%s: error creating send thread - disabling LASH: %s\n",
290 				__FUNCTION__, strerror(err));
291 
292 		client->recv_close = 1;
293 		pthread_join(client->recv_thread, NULL);
294 
295 		lash_client_destroy(client);
296 		return NULL;
297 	}
298 
299 	return client;
300 }
301 
302 unsigned int
lash_get_pending_event_count(lash_client_t * client)303 lash_get_pending_event_count(lash_client_t * client)
304 {
305 	unsigned int count = 0;
306 
307 	if (!client)
308 		return 0;
309 
310 	pthread_mutex_lock(&client->events_in_lock);
311 
312 	if (client->events_in)
313 		count = lash_list_length(client->events_in);
314 
315 	pthread_mutex_unlock(&client->events_in_lock);
316 
317 	return count;
318 }
319 
320 unsigned int
lash_get_pending_config_count(lash_client_t * client)321 lash_get_pending_config_count(lash_client_t * client)
322 {
323 	unsigned int count = 0;
324 
325 	if (!lash_enabled(client))
326 		return 0;
327 
328 	pthread_mutex_lock(&client->configs_in_lock);
329 
330 	if (client->events_in)
331 		count = lash_list_length(client->configs_in);
332 
333 	pthread_mutex_unlock(&client->configs_in_lock);
334 
335 	return count;
336 }
337 
338 lash_event_t *
lash_get_event(lash_client_t * client)339 lash_get_event(lash_client_t * client)
340 {
341 	lash_event_t *event = NULL;
342 
343 	if (!client)
344 		return NULL;
345 
346 	pthread_mutex_lock(&client->events_in_lock);
347 
348 	if (client->events_in) {
349 		event = (lash_event_t *) client->events_in->data;
350 		client->events_in = lash_list_remove(client->events_in, event);
351 	}
352 
353 	pthread_mutex_unlock(&client->events_in_lock);
354 
355 	return event;
356 }
357 
358 lash_config_t *
lash_get_config(lash_client_t * client)359 lash_get_config(lash_client_t * client)
360 {
361 	lash_config_t *config = NULL;
362 
363 	if (!client)
364 		return NULL;
365 
366 	pthread_mutex_lock(&client->configs_in_lock);
367 
368 	if (client->configs_in) {
369 		config = (lash_config_t *) client->configs_in->data;
370 		client->configs_in = lash_list_remove(client->configs_in, config);
371 	}
372 
373 	pthread_mutex_unlock(&client->configs_in_lock);
374 
375 	return config;
376 }
377 
378 void
lash_send_comm_event(lash_client_t * client,lash_comm_event_t * event)379 lash_send_comm_event(lash_client_t * client, lash_comm_event_t * event)
380 {
381 	pthread_mutex_lock(&client->comm_events_out_lock);
382 	client->comm_events_out =
383 		lash_list_append(client->comm_events_out, event);
384 	pthread_mutex_unlock(&client->comm_events_out_lock);
385 	pthread_cond_signal(&client->send_conditional);
386 }
387 
388 void
lash_send_event(lash_client_t * client,lash_event_t * event)389 lash_send_event(lash_client_t * client, lash_event_t * event)
390 {
391 	lash_comm_event_t *comm_event;
392 
393 	if (!lash_enabled(client)) {
394 		lash_event_destroy(event);
395 		return;
396 	}
397 
398 	comm_event = lash_malloc(sizeof(lash_comm_event_t));
399 	comm_event->type = LASH_Comm_Event_Event;
400 	comm_event->event_data.event = event;
401 
402 	lash_send_comm_event(client, comm_event);
403 }
404 
405 void
lash_send_config(lash_client_t * client,lash_config_t * config)406 lash_send_config(lash_client_t * client, lash_config_t * config)
407 {
408 	lash_comm_event_t *comm_event;
409 
410 	if (!lash_enabled(client)) {
411 		lash_config_destroy(config);
412 		return;
413 	}
414 
415 	comm_event = lash_malloc(sizeof(lash_comm_event_t));
416 	comm_event->type = LASH_Comm_Event_Config;
417 	comm_event->event_data.config = config;
418 
419 	lash_send_comm_event(client, comm_event);
420 }
421 
422 const char *
lash_get_server_name(lash_client_t * client)423 lash_get_server_name(lash_client_t * client)
424 {
425 	if (!lash_enabled(client))
426 		return NULL;
427 
428 	return lash_lookup_peer_name(client->socket);
429 }
430 
431 int
lash_server_connected(lash_client_t * client)432 lash_server_connected(lash_client_t * client)
433 {
434 	if (!client)
435 		return 0;
436 
437 	return client->server_connected;
438 }
439 
440 void
lash_jack_client_name(lash_client_t * client,const char * name)441 lash_jack_client_name(lash_client_t * client, const char *name)
442 {
443 	lash_event_t *event;
444 
445 	if (!lash_enabled(client))
446 		return;
447 
448 	if (!name)
449 		return;
450 
451 	event = lash_event_new_with_all(LASH_Jack_Client_Name, name);
452 
453 	lash_send_event(client, event);
454 }
455 
456 void
lash_alsa_client_id(lash_client_t * client,unsigned char id)457 lash_alsa_client_id(lash_client_t * client, unsigned char id)
458 {
459 	lash_event_t *event;
460 
461 	if (!lash_enabled(client))
462 		return;
463 
464 	if (!id)
465 		return;
466 
467 	event = lash_event_new();
468 	lash_event_set_alsa_client_id(event, id);
469 
470 	lash_send_event(client, event);
471 }
472 
473 /* EOF */
474