Fixes for lash_args (duplicated, documentation clarification, etc)

git-svn-id: svn://svn.savannah.nongnu.org/lash/trunk@37 1de19dc7-4e3f-0410-a61d-eddf686bf0b7
This commit is contained in:
Dave Robillard 2006-05-22 04:00:39 +00:00
parent 0db0243105
commit dea4042568
6 changed files with 107 additions and 54 deletions

View File

@ -319,6 +319,9 @@ Doing this you can simply put @command{lash_panel} (or any other LASH
client) in your applications menu and have LASH automatically work without
having to remember to start the server manually.
Starting the server can be disabled by specifying the
@option{--lash-no-start-server} option on the client's command line.
If you're using a Bourne compatible shell like @command{bash} (if you don't
know, you probably are) you can enable auto-start with the following command:
@ -526,6 +529,7 @@ should not normally be set by clients themselves.
The client is dependant on being run in a terminal.
@item LASH_No_Start_Server
Do not attempt to start LASH server.
This flag can be set by the @command{--lash-no-start-server} command line option.
@end table
The @var{protocol} argument should be the version of the high-level protocol that the client

View File

@ -41,7 +41,8 @@ struct _lash_args
void lash_args_free (lash_args_t * args);
lash_args_t * lash_args_new ();
void lash_args_destroy (lash_args_t * args);
lash_args_t * lash_args_duplicate (const lash_args_t *const src);
void lash_args_destroy (lash_args_t * args);
void lash_args_set_project (lash_args_t * args, const char * project);
void lash_args_set_server (lash_args_t * args, const char * server);

View File

@ -42,7 +42,7 @@ void lash_args_destroy (lash_args_t* args);
/** open a connection to the server
* returns NULL on failure
*/
lash_client_t * lash_init (lash_args_t * args, const char * client_class, int client_flags, lash_protocol_t protocol);
lash_client_t * lash_init (const lash_args_t * args, const char * client_class, int client_flags, lash_protocol_t protocol);
/** get the hostname of the server
*/

View File

@ -53,10 +53,41 @@ lash_args_new()
lash_args_t *args;
args = lash_malloc0(sizeof(lash_args_t));
args->project = NULL;
args->server = NULL;
uuid_clear(args->id);
args->flags = 0;
args->argc = 0;
args->argv = NULL;
return args;
}
lash_args_t *
lash_args_duplicate(const lash_args_t *const src)
{
if (src == NULL)
return NULL;
lash_args_t* result = lash_args_new();
if (src->project)
result->project = lash_strdup(src->project);
if (src->server)
result->server = lash_strdup(src->server);
if (!uuid_is_null(src->id))
uuid_copy(result->id, src->id);
result->flags = src->flags;
result->argc = 0;
result->argv = NULL;
if (result->argc > 0 && result->argv)
lash_args_set_args(result, src->argc, src->argv);
return result;
}
void
lash_args_destroy(lash_args_t * args)
{
@ -154,4 +185,5 @@ lash_args_get_argv(const lash_args_t * args)
return (const char *const *)args->argv;
}
/* EOF */

View File

@ -169,7 +169,7 @@ lash_comm_recv_finish(lash_client_t * client)
pthread_join(client->send_thread, NULL);
close(client->socket);
lash_args_destroy(client->args);
/*lash_args_destroy(client->args);*/
client->args = NULL;
free(client->class);
client->class = NULL;

View File

@ -92,6 +92,17 @@ lash_extract_args(int *argc, char ***argv)
continue;
}
if (strncmp("--lash-no-start-server", (*argv)[i], 22) == 0) {
LASH_PRINT_DEBUG
("setting LASH_No_Start_Server flag from command line");
lash_args_set_flag(args, LASH_No_Start_Server);
(*argv)[i] = NULL;
continue;
}
}
LASH_PRINT_DEBUG("args checked");
@ -120,7 +131,7 @@ lash_extract_args(int *argc, char ***argv)
}
lash_client_t *
lash_init(lash_args_t * args,
lash_init(const lash_args_t * args,
const char *class, int client_flags, lash_protocol_t protocol)
{
lash_connect_params_t *connect_params;
@ -135,7 +146,7 @@ lash_init(lash_args_t * args,
client = lash_client_new();
connect_params = lash_connect_params_new();
client->args = args;
client->args = lash_args_duplicate(args);
client->args->flags |= client_flags;
lash_client_set_class(client, class);
@ -173,66 +184,71 @@ lash_init(lash_args_t * args,
/* couldn't connect, try to start a new server */
/* but not if this client has been started by a server, in which
case something must be broken if we can't connect */
lash_args_get_id(args, id);
if (err && getenv("LASH_START_SERVER") != NULL && uuid_is_null(id)) {
LASH_DEBUGARGS("%s: trying to start new LASH server\n",
__FUNCTION__);
/* using the same double fork() trick as JACK does to prevent
zombie children */
err = fork();
/* child process will run this statement */
if (err == 0) {
switch (fork()) {
/* grandchild process will run this block */
case 0:
setsid();
execlp("lashd", "lashd", NULL);
_exit(-1);
/* this block only runs if the second fork() fails */
case -1:
_exit (-1);
/* exit the child process here */
default:
_exit (0);
}
}
if ( !(client_flags | LASH_No_Start_Server) ) {
lash_args_get_id(args, id);
if (err && getenv("LASH_START_SERVER") != NULL && uuid_is_null(id)) {
LASH_DEBUGARGS("%s: trying to start new LASH server\n",
__FUNCTION__);
/* if the fork succeeded, try to connect to the new server */
else if (err > 0) {
waitpid(err, NULL, 0);
for (tries = 0; tries < 5; ++tries) {
sleep(1);
err = lash_comm_connect_to_server(client,
cstr ? cstr : "localhost",
"lash", connect_params);
if (err == 0) {
LASH_PRINT_DEBUG("successfully launched and connected to lashd");
break;
/* using the same double fork() trick as JACK does to prevent
zombie children */
err = fork();
/* child process will run this statement */
if (err == 0) {
switch (fork()) {
/* grandchild process will run this block */
case 0:
setsid();
execlp("lashd", "lashd", NULL);
_exit(-1);
/* this block only runs if the second fork() fails */
case -1:
_exit (-1);
/* exit the child process here */
default:
_exit (0);
}
}
/* fork failed */
} else {
fprintf(stderr, "%s: fork failed while starting new server: %s\n",
__FUNCTION__, strerror(err));
/* if the fork succeeded, try to connect to the new server */
else if (err > 0) {
waitpid(err, NULL, 0);
for (tries = 0; tries < 5; ++tries) {
sleep(1);
err = lash_comm_connect_to_server(client,
cstr ? cstr : "localhost",
"lash", connect_params);
if (err == 0) {
LASH_PRINT_DEBUG("successfully launched and connected to lashd");
break;
}
}
/* fork failed */
} else {
fprintf(stderr, "%s: fork failed while starting new server: %s\n",
__FUNCTION__, strerror(err));
}
} else {
fprintf(stderr, "%s: Not attempting to start daemon server automatically\n",
__FUNCTION__, __FUNCTION__);
}
}
lash_connect_params_destroy(connect_params);
// this deletes the contained strings, but we don't want to do that
// since they point to the strings in args
//lash_connect_params_destroy(connect_params);
connect_params = NULL;
if (err) {
fprintf(stderr,
"%s: could not connect to server '%s' - disabling LASH\n",
__FUNCTION__, cstr ? cstr : "localhost");
if (getenv("LAST_START_SERVER") == NULL)
fprintf(stderr, "%s: LASH_START_SERVER unset, not attempting to start"
" server automatically\n",
__FUNCTION__, __FUNCTION__);
lash_client_destroy(client);
return NULL;
}