Skip to content

Commit

Permalink
Merge pull request #122 from marcone/params2
Browse files Browse the repository at this point in the history
Rework command line argument handling
  • Loading branch information
jfdelnero authored Nov 19, 2024
2 parents cc2b7d7 + 6a9fb89 commit 489ba42
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 26 deletions.
4 changes: 3 additions & 1 deletion inc/msgqueue.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <mqueue.h>

int msgqueue_handler_init( mtp_ctx * ctx );
int msgqueue_handler_deinit( mtp_ctx * ctx );

mqd_t get_message_queue();
int send_message_queue( char * message );
28 changes: 15 additions & 13 deletions src/msgqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,24 @@ static void* msgqueue_thread( void* arg )
return NULL;
}

mqd_t get_message_queue() {
mqd_t qid = mq_open("/umtprd", O_RDWR | O_CREAT, 0600, &qattrs);
if (qid < 0)
{
PRINT_ERROR("%s : mq_open error %d (%s)", __func__, errno, strerror(errno));
}
else
{
PRINT_DEBUG("%s : msgqueue_id = %d", __func__, qid);
}
return qid;
}

int send_message_queue(char * message )
{
mqd_t msgqueue_id;

msgqueue_id = mq_open("/umtprd", O_RDWR | O_CREAT, 0600, &qattrs);
PRINT_DEBUG("%s : msgqueue_id = %d", __func__, msgqueue_id);
msgqueue_id = get_message_queue();
if (msgqueue_id != -1)
{
if (mq_send(msgqueue_id, message, strlen(message) + 1, 0) == 0 )
Expand All @@ -261,10 +273,6 @@ int send_message_queue(char * message )
PRINT_ERROR("%s : Couldn't send %s !", __func__, message);
}
}
else
{
PRINT_ERROR("%s : mq_open error %d (%s)", __func__, errno, strerror(errno));
}

return -1;
}
Expand All @@ -275,11 +283,9 @@ int msgqueue_handler_init(mtp_ctx * ctx )

if( ctx )
{
ctx->msgqueue_id = mq_open("/umtprd", O_RDWR | O_CREAT, 0600, &qattrs);
ctx->msgqueue_id = get_message_queue();
if(ctx->msgqueue_id != -1)
{
PRINT_DEBUG("%s : msgqueue_id = %d", __func__, ctx->msgqueue_id);

ret = pthread_create(&ctx->msgqueue_thread, NULL, msgqueue_thread, ctx);
if(ret != 0)
{
Expand All @@ -291,10 +297,6 @@ int msgqueue_handler_init(mtp_ctx * ctx )
return 0;
}
}
else
{
PRINT_ERROR("%s : mq_open error %d (%s)", __func__, errno, strerror(errno));
}
}

return -2;
Expand Down
72 changes: 60 additions & 12 deletions src/umtprd.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <pthread.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/file.h>

#ifdef SYSTEMD_NOTIFY
#include <systemd/sd-login.h>
Expand Down Expand Up @@ -139,32 +140,79 @@ int main(int argc, char *argv[])
{
const char *conffile = UMTPR_CONF_FILE;
int retcode;
mqd_t fd = -1;
int already_running = 0;

PRINT_MSG("uMTP Responder");
PRINT_MSG("Version: %s compiled the %s@%s", APP_VERSION,
PRINT_MSG("Version: %s compiled %s@%s", APP_VERSION,
__DATE__, __TIME__);

PRINT_MSG("(c) 2018 - 2024 Viveris Technologies");

if(argc>1)
fd = get_message_queue();
if (fd < 0)
{
if(argv[1])
exit(1);
}
retcode = flock(fd, LOCK_EX | LOCK_NB);
if (retcode)
{
// lock failed, umtprd is already running
already_running = 1;
}

if (argc <= 1 && already_running)
{
PRINT_MSG("Already running");
return 0;
}

while(argc > 1)
{
if(!strncmp(argv[1], PARAMETER_IPCCMD, sizeof(PARAMETER_IPCCMD) - 1))
{
if(!strncmp(argv[1],PARAMETER_IPCCMD,sizeof(PARAMETER_IPCCMD)-1))
char *cmd = &argv[1][sizeof(PARAMETER_IPCCMD) - 1];
PRINT_MSG("Sending command : %s", cmd);
retcode = send_message_queue(cmd);
if (retcode)
{
PRINT_MSG("Sending command : %s",&argv[1][sizeof(PARAMETER_IPCCMD)-1]);
retcode = send_message_queue( &argv[1][sizeof(PARAMETER_IPCCMD)-1] );
PRINT_ERROR("Error (%d) sending '%s'", retcode, cmd);
exit(retcode);
}

if(!strcmp(argv[1],PARAMETER_CONF) && argc > 2)
conffile = argv[2];
argc--;
argv++;
}
else if(!strcmp(argv[1], PARAMETER_CONF))
{
if (already_running)
{
PRINT_ERROR("Can't set config file when %s is already running", argv[0]);
exit(1);
}
if (argc < 3)
{
PRINT_ERROR("%s option requires file", PARAMETER_CONF);
exit(1);
}
PRINT_MSG("Using config file %s", argv[2]);
conffile = argv[2];
argc -= 2;
argv += 2;
}
else
{
PRINT_ERROR("Unknown command line option: %s", argv[1]);
exit(1);
}
}

retcode = main_thread(conffile);
if( retcode )
PRINT_ERROR("Error : Couldn't run the main thread... (%d)", retcode);
if (!already_running)
{
PRINT_DEBUG("starting main thread");
retcode = main_thread(conffile);
if( retcode )
PRINT_ERROR("Error : Couldn't run the main thread... (%d)", retcode);
};

return -retcode;
}

0 comments on commit 489ba42

Please sign in to comment.