Created
March 15, 2025 16:52
-
-
Save darealshinji/6848fd417c57c5298eef96696ce82b80 to your computer and use it in GitHub Desktop.
check if another instance of this program is already running
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Linux: check if another instance of this program is already running | |
* by looking through the /proc/<PID>/ entries */ | |
/** | |
* MIT License | |
* | |
* Copyright (C) 2024 Carsten Janssen | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
#include <ctype.h> | |
#include <dirent.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
/** | |
* return values: | |
* -1 error | |
* 0 this is the first instance | |
* >0 PID of the other running instance | |
*/ | |
long simple_instance_check() | |
{ | |
DIR *d; | |
struct dirent *dir; | |
char *self; | |
char buf[64]; | |
long pid_self, rv = -1; | |
memset(buf, 0, sizeof(buf)); | |
pid_self = (long)getpid(); | |
if ((self = realpath("/proc/self/exe", NULL)) == NULL) { | |
return -1; | |
} | |
if ((d = opendir("/proc")) == NULL) { | |
return -1; | |
} | |
while ((dir = readdir(d)) != NULL) { | |
char *rp, *endptr; | |
long pid; | |
/* looking for a directory */ | |
if (dir->d_type != DT_DIR && dir->d_type != DT_UNKNOWN) { | |
continue; | |
} | |
pid = strtol(dir->d_name, &endptr, 10); | |
/* name should be numbers only, ignore our own PID */ | |
if (*endptr != 0 || pid == pid_self) { | |
continue; | |
} | |
/* get executable path */ | |
snprintf(buf, sizeof(buf)-1, "/proc/%ld/exe", pid); | |
if ((rp = realpath(buf, NULL)) == NULL) { | |
continue; | |
} | |
/* compare paths */ | |
if (strcmp(rp, self) == 0) { | |
rv = pid; | |
free(rp); | |
goto JMP_EXIT; | |
} | |
free(rp); | |
} | |
rv = 0; | |
JMP_EXIT: | |
closedir(d); | |
free(self); | |
return rv; | |
} | |
int main() | |
{ | |
long l = simple_instance_check(); | |
if (l == -1) { | |
perror("simple_instance_check()"); | |
return 1; | |
} else if (l > 0) { | |
fprintf(stderr, "another instance is already running: PID %ld\n", l); | |
return 1; | |
} | |
printf("PID %ld\n", (long)getpid()); | |
puts("press ENTER"); | |
(void)fgetc(stdin); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment