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?










share|improve this question







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















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?










share|improve this question







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













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?










share|improve this question







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






share|improve this question







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.











share|improve this question







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.









share|improve this question




share|improve this question






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


















  • 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















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Michael B. is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















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






























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.










draft saved

draft discarded


















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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno