forked from sanikoyes/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
*.o | ||
*.a | ||
skynet | ||
skynet.pid | ||
3rd/lua/lua | ||
3rd/lua/luac | ||
cservice | ||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
#include <sys/file.h> | ||
#include <signal.h> | ||
#include <errno.h> | ||
|
||
#include "skynet_daemon.h" | ||
|
||
static int | ||
check_pid(const char *pidfile) { | ||
int pid = 0; | ||
FILE *f = fopen(pidfile,"r"); | ||
if (f == NULL) | ||
return 0; | ||
int n = fscanf(f,"%d", &pid); | ||
fclose(f); | ||
|
||
if (n !=1 || pid == 0 || pid == getpid()) { | ||
return 0; | ||
} | ||
|
||
if (kill(pid, 0) && errno == ESRCH) | ||
return 0; | ||
|
||
return pid; | ||
} | ||
|
||
static int | ||
write_pid(const char *pidfile) { | ||
FILE *f; | ||
int pid = 0; | ||
int fd = open(pidfile, O_RDWR|O_CREAT, 0644); | ||
if (fd == -1) { | ||
fprintf(stderr, "Can't create %s.\n", pidfile); | ||
return 0; | ||
} | ||
f = fdopen(fd, "r+"); | ||
if (f == NULL) { | ||
fprintf(stderr, "Can't open %s.\n", pidfile); | ||
return 0; | ||
} | ||
|
||
if (flock(fd, LOCK_EX|LOCK_NB) == -1) { | ||
int n = fscanf(f, "%d", &pid); | ||
fclose(f); | ||
if (n != 1) { | ||
fprintf(stderr, "Can't lock and read pidfile.\n"); | ||
} else { | ||
fprintf(stderr, "Can't lock pidfile, lock is held by pid %d.\n", pid); | ||
} | ||
return 0; | ||
} | ||
|
||
pid = getpid(); | ||
if (!fprintf(f,"%d\n", pid)) { | ||
fprintf(stderr, "Can't write pid.\n"); | ||
close(fd); | ||
return 0; | ||
} | ||
fflush(f); | ||
|
||
if (flock(fd, LOCK_UN) == -1) { | ||
fprintf(stderr, "Can't unlock pidfile %s.\n", pidfile); | ||
close(fd); | ||
return 0; | ||
} | ||
close(fd); | ||
|
||
return pid; | ||
} | ||
|
||
int | ||
daemon_init(const char *pidfile) { | ||
int pid = check_pid(pidfile); | ||
if (pid) { | ||
fprintf(stderr, "Skynet is already running, pid = %d.\n", pid); | ||
return 1; | ||
} | ||
|
||
if (daemon(1,0)) { | ||
fprintf(stderr, "Can't daemonize.\n"); | ||
return 1; | ||
} | ||
|
||
pid = write_pid(pidfile); | ||
if (pid == 0) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int | ||
daemon_exit(const char *pidfile) { | ||
return unlink (pidfile); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef skynet_daemon_h | ||
#define skynet_daemon_h | ||
|
||
int daemon_init(const char *pidfile); | ||
int daemon_exit(const char *pidfile); | ||
|
||
#endif |
This file contains 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
This file contains 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
This file contains 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