1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
From: Roman Yepishev <roman.yepishev@gmail.com>
Subject: osdctl now checks that osdsh is running, osdsh checks that another
Subject: instance is not running before starting up
--- a/src/osdctl/osdctl.c
+++ b/src/osdctl/osdctl.c
@@ -36,11 +36,31 @@
{
FILE *fp;
char fifo_file[PATH_MAX];
+ char pid_file[PATH_MAX];
+ pid_t osdsh_pid;
if (strcmp(command, "")==0)
return;
sprintf(fifo_file, "%s.%d", OSD_FIFO_PATH, getuid());
+ sprintf(pid_file, "%s.%d.pid", OSD_FIFO_PATH, getuid());
+
+ /*
+ * We need to check that osdSH is really running because writing
+ * to the socket without osdSH reading from it results in osdctl
+ * hang
+ */
+ if((fp = fopen(pid_file, "r")) == NULL) {
+ perror("osdSH is not running (no pid file)");
+ exit(1);
+ }
+
+ if((!fscanf(fp, "%d", &osdsh_pid)) || kill(osdsh_pid, 0)) {
+ perror("osdSH is not running");
+ exit(1);
+ }
+
+ fclose(fp);
if((fp = fopen(fifo_file, "w")) == NULL) {
perror("Couldn't open fifo");
--- a/src/osdctl/osdctl.h
+++ b/src/osdctl/osdctl.h
@@ -1,3 +1,6 @@
#include <stdio.h>
#include <getopt.h>
#include <linux/limits.h>
+#include <sys/types.h>
+#include <signal.h>
+
--- a/src/osdsh/osdsh.c
+++ b/src/osdsh/osdsh.c
@@ -149,6 +149,7 @@
int main(int argc, char *argv[], char *env[])
{
pid_t childpid;
+ pid_t osdsh_pid;
char pid_file[PATH_MAX+1];
FILE *fp;
@@ -169,6 +170,16 @@
pppset = mixerset = settings;
*/
+ sprintf(pid_file, "%s.%d.pid", OSD_FIFO_PATH, getuid()) ;
+
+ if(fp = fopen(pid_file, "r")) {
+ if((fscanf(fp, "%d", &osdsh_pid)) && !kill(osdsh_pid, 0)) {
+ fprintf(stderr, "osdSH is already running (pid %d)\n", osdsh_pid);
+ exit(1);
+ }
+ fclose(fp);
+ }
+
if((childpid=fork())<0) {
perror("fork");
exit(1);
@@ -177,7 +188,6 @@
control_sh(NULL);
}
else {
- sprintf(pid_file, "%s.%d.pid", OSD_FIFO_PATH, getuid()) ;
fp=fopen(pid_file, "w");
fprintf(fp, "%d", childpid);
fclose(fp);
--- a/src/osdsh/osdsh.h
+++ b/src/osdsh/osdsh.h
@@ -10,6 +10,7 @@
#include <fcntl.h>
#include <pthread.h>
#include <dlfcn.h>
+#include <signal.h>
#include <linux/limits.h>
#include <linux/stat.h>
|