Block same instance of a program for running again
up vote
2
down vote
favorite
I am implementing a library (Linux) and I created some functions to block new instances of a running program and I was wonder if there are some better improvements for it.
Here is a program which describes what I am trying:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define SECONDS 20
static char *program_path = NULL;
char *my_strtok ( char *const msg, const char *const ch );
void block_new_instance ( const char *const instance );
void clean_instance ( void );
int main ( int argc, char *argv )
{
if ( argc != 1 )
{
printf( "nt*** Arguments are NOT allowed. ***n" );
exit( EXIT_FAILURE );
}
block_new_instance( argv[ 0 ] );
sleep( SECONDS );
}
void block_new_instance( const char *const instance )
{
char prog_name[ strlen( instance ) + 1 ];
memset( prog_name, '', sizeof( prog_name ) );
strcpy( prog_name, instance );
char *buffer = my_strtok( prog_name, "/" );
struct flock file_lock;
char *dir = getenv( "HOME" );
if ( dir == NULL || dir[0] != '/' )
{
fprintf( stderr, "Wrong Directory, getenv(): %s (%d)n", strerror( errno ), errno );
exit( EXIT_FAILURE );
}
program_path = calloc( sizeof ( *program_path ), strlen( dir ) + ( strlen( buffer ) + sizeof ( "/" ) ) );
if ( program_path == NULL )
{
printf( "Error, malloc()n" );
exit ( EXIT_FAILURE );
}
memcpy( program_path, dir, strlen( dir ) );
memcpy( program_path + strlen( dir ), "/", sizeof( "/") );
memcpy( program_path + ( strlen( dir ) + strlen( "/" ) ), buffer, strlen( buffer ) );
int file_desk = open( program_path, O_RDWR | O_CREAT, 0600 );
if ( file_desk < 0 )
{
fprintf( stderr, "open: %s (%d)n", strerror( errno ), errno );
exit( EXIT_FAILURE );
}
file_lock.l_start = 0;
file_lock.l_len = 0;
file_lock.l_type = F_WRLCK;
file_lock.l_whence = SEEK_SET;
if ( fcntl( file_desk, F_SETLK, &file_lock ) < 0 )
{
fprintf( stderr, "%s is already runningn", buffer );
exit( EXIT_FAILURE );
}
atexit( clean_instance );
}
char *my_strtok( char *const msg, const char *const ch )
{
char *ret = NULL;
char *tmp = strtok( msg, ch );
while ( tmp != NULL )
{
ret = tmp;
tmp = strtok( NULL, ch );
}
if ( ret == NULL )
{
return NULL;
}
return ret;
}
void clean_instance( void )
{
unlink ( program_path );
free ( program_path );
}
Possible Outputs are:
*** Arguments are NOT allowed. **
or:
Program is already running
I would like to know which improvements are needed?
c linux
New contributor
Michael B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
2
down vote
favorite
I am implementing a library (Linux) and I created some functions to block new instances of a running program and I was wonder if there are some better improvements for it.
Here is a program which describes what I am trying:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define SECONDS 20
static char *program_path = NULL;
char *my_strtok ( char *const msg, const char *const ch );
void block_new_instance ( const char *const instance );
void clean_instance ( void );
int main ( int argc, char *argv )
{
if ( argc != 1 )
{
printf( "nt*** Arguments are NOT allowed. ***n" );
exit( EXIT_FAILURE );
}
block_new_instance( argv[ 0 ] );
sleep( SECONDS );
}
void block_new_instance( const char *const instance )
{
char prog_name[ strlen( instance ) + 1 ];
memset( prog_name, '', sizeof( prog_name ) );
strcpy( prog_name, instance );
char *buffer = my_strtok( prog_name, "/" );
struct flock file_lock;
char *dir = getenv( "HOME" );
if ( dir == NULL || dir[0] != '/' )
{
fprintf( stderr, "Wrong Directory, getenv(): %s (%d)n", strerror( errno ), errno );
exit( EXIT_FAILURE );
}
program_path = calloc( sizeof ( *program_path ), strlen( dir ) + ( strlen( buffer ) + sizeof ( "/" ) ) );
if ( program_path == NULL )
{
printf( "Error, malloc()n" );
exit ( EXIT_FAILURE );
}
memcpy( program_path, dir, strlen( dir ) );
memcpy( program_path + strlen( dir ), "/", sizeof( "/") );
memcpy( program_path + ( strlen( dir ) + strlen( "/" ) ), buffer, strlen( buffer ) );
int file_desk = open( program_path, O_RDWR | O_CREAT, 0600 );
if ( file_desk < 0 )
{
fprintf( stderr, "open: %s (%d)n", strerror( errno ), errno );
exit( EXIT_FAILURE );
}
file_lock.l_start = 0;
file_lock.l_len = 0;
file_lock.l_type = F_WRLCK;
file_lock.l_whence = SEEK_SET;
if ( fcntl( file_desk, F_SETLK, &file_lock ) < 0 )
{
fprintf( stderr, "%s is already runningn", buffer );
exit( EXIT_FAILURE );
}
atexit( clean_instance );
}
char *my_strtok( char *const msg, const char *const ch )
{
char *ret = NULL;
char *tmp = strtok( msg, ch );
while ( tmp != NULL )
{
ret = tmp;
tmp = strtok( NULL, ch );
}
if ( ret == NULL )
{
return NULL;
}
return ret;
}
void clean_instance( void )
{
unlink ( program_path );
free ( program_path );
}
Possible Outputs are:
*** Arguments are NOT allowed. **
or:
Program is already running
I would like to know which improvements are needed?
c linux
New contributor
Michael B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
When/Why would you use this? More context would help with figuring out possible (unwanted) side-effects.
– Mast
1 hour ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am implementing a library (Linux) and I created some functions to block new instances of a running program and I was wonder if there are some better improvements for it.
Here is a program which describes what I am trying:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define SECONDS 20
static char *program_path = NULL;
char *my_strtok ( char *const msg, const char *const ch );
void block_new_instance ( const char *const instance );
void clean_instance ( void );
int main ( int argc, char *argv )
{
if ( argc != 1 )
{
printf( "nt*** Arguments are NOT allowed. ***n" );
exit( EXIT_FAILURE );
}
block_new_instance( argv[ 0 ] );
sleep( SECONDS );
}
void block_new_instance( const char *const instance )
{
char prog_name[ strlen( instance ) + 1 ];
memset( prog_name, '', sizeof( prog_name ) );
strcpy( prog_name, instance );
char *buffer = my_strtok( prog_name, "/" );
struct flock file_lock;
char *dir = getenv( "HOME" );
if ( dir == NULL || dir[0] != '/' )
{
fprintf( stderr, "Wrong Directory, getenv(): %s (%d)n", strerror( errno ), errno );
exit( EXIT_FAILURE );
}
program_path = calloc( sizeof ( *program_path ), strlen( dir ) + ( strlen( buffer ) + sizeof ( "/" ) ) );
if ( program_path == NULL )
{
printf( "Error, malloc()n" );
exit ( EXIT_FAILURE );
}
memcpy( program_path, dir, strlen( dir ) );
memcpy( program_path + strlen( dir ), "/", sizeof( "/") );
memcpy( program_path + ( strlen( dir ) + strlen( "/" ) ), buffer, strlen( buffer ) );
int file_desk = open( program_path, O_RDWR | O_CREAT, 0600 );
if ( file_desk < 0 )
{
fprintf( stderr, "open: %s (%d)n", strerror( errno ), errno );
exit( EXIT_FAILURE );
}
file_lock.l_start = 0;
file_lock.l_len = 0;
file_lock.l_type = F_WRLCK;
file_lock.l_whence = SEEK_SET;
if ( fcntl( file_desk, F_SETLK, &file_lock ) < 0 )
{
fprintf( stderr, "%s is already runningn", buffer );
exit( EXIT_FAILURE );
}
atexit( clean_instance );
}
char *my_strtok( char *const msg, const char *const ch )
{
char *ret = NULL;
char *tmp = strtok( msg, ch );
while ( tmp != NULL )
{
ret = tmp;
tmp = strtok( NULL, ch );
}
if ( ret == NULL )
{
return NULL;
}
return ret;
}
void clean_instance( void )
{
unlink ( program_path );
free ( program_path );
}
Possible Outputs are:
*** Arguments are NOT allowed. **
or:
Program is already running
I would like to know which improvements are needed?
c linux
New contributor
Michael B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am implementing a library (Linux) and I created some functions to block new instances of a running program and I was wonder if there are some better improvements for it.
Here is a program which describes what I am trying:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define SECONDS 20
static char *program_path = NULL;
char *my_strtok ( char *const msg, const char *const ch );
void block_new_instance ( const char *const instance );
void clean_instance ( void );
int main ( int argc, char *argv )
{
if ( argc != 1 )
{
printf( "nt*** Arguments are NOT allowed. ***n" );
exit( EXIT_FAILURE );
}
block_new_instance( argv[ 0 ] );
sleep( SECONDS );
}
void block_new_instance( const char *const instance )
{
char prog_name[ strlen( instance ) + 1 ];
memset( prog_name, '', sizeof( prog_name ) );
strcpy( prog_name, instance );
char *buffer = my_strtok( prog_name, "/" );
struct flock file_lock;
char *dir = getenv( "HOME" );
if ( dir == NULL || dir[0] != '/' )
{
fprintf( stderr, "Wrong Directory, getenv(): %s (%d)n", strerror( errno ), errno );
exit( EXIT_FAILURE );
}
program_path = calloc( sizeof ( *program_path ), strlen( dir ) + ( strlen( buffer ) + sizeof ( "/" ) ) );
if ( program_path == NULL )
{
printf( "Error, malloc()n" );
exit ( EXIT_FAILURE );
}
memcpy( program_path, dir, strlen( dir ) );
memcpy( program_path + strlen( dir ), "/", sizeof( "/") );
memcpy( program_path + ( strlen( dir ) + strlen( "/" ) ), buffer, strlen( buffer ) );
int file_desk = open( program_path, O_RDWR | O_CREAT, 0600 );
if ( file_desk < 0 )
{
fprintf( stderr, "open: %s (%d)n", strerror( errno ), errno );
exit( EXIT_FAILURE );
}
file_lock.l_start = 0;
file_lock.l_len = 0;
file_lock.l_type = F_WRLCK;
file_lock.l_whence = SEEK_SET;
if ( fcntl( file_desk, F_SETLK, &file_lock ) < 0 )
{
fprintf( stderr, "%s is already runningn", buffer );
exit( EXIT_FAILURE );
}
atexit( clean_instance );
}
char *my_strtok( char *const msg, const char *const ch )
{
char *ret = NULL;
char *tmp = strtok( msg, ch );
while ( tmp != NULL )
{
ret = tmp;
tmp = strtok( NULL, ch );
}
if ( ret == NULL )
{
return NULL;
}
return ret;
}
void clean_instance( void )
{
unlink ( program_path );
free ( program_path );
}
Possible Outputs are:
*** Arguments are NOT allowed. **
or:
Program is already running
I would like to know which improvements are needed?
c linux
c linux
New contributor
Michael B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Michael B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Michael B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 hours ago
Michael B.
111
111
New contributor
Michael B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Michael B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Michael B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
When/Why would you use this? More context would help with figuring out possible (unwanted) side-effects.
– Mast
1 hour ago
add a comment |
When/Why would you use this? More context would help with figuring out possible (unwanted) side-effects.
– Mast
1 hour ago
When/Why would you use this? More context would help with figuring out possible (unwanted) side-effects.
– Mast
1 hour ago
When/Why would you use this? More context would help with figuring out possible (unwanted) side-effects.
– Mast
1 hour ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Michael B. is a new contributor. Be nice, and check out our Code of Conduct.
Michael B. is a new contributor. Be nice, and check out our Code of Conduct.
Michael B. is a new contributor. Be nice, and check out our Code of Conduct.
Michael B. is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209573%2fblock-same-instance-of-a-program-for-running-again%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When/Why would you use this? More context would help with figuring out possible (unwanted) side-effects.
– Mast
1 hour ago