- advisory file locking in the cfgfile read/write code

- changes in the init script to use the pidfile for killing
- changed the spnavcfg program, added functionality to start/stop the X11
  magellan interface, and to "ping" the server and see if it's running.


git-svn-id: svn+ssh://svn.code.sf.net/p/spacenav/code/spacenavd@23 ef983eb1-d774-4af8-acfd-baaf7b16a646
pull/1/head
John Tsiombikas 2008-04-09 04:27:12 +00:00
rodzic 27d0aab0ea
commit d8f197d7e0
4 zmienionych plików z 46 dodań i 7 usunięć

Wyświetl plik

@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include "cfgfile.h"
enum {TX, TY, TZ, RX, RY, RZ};
@ -35,6 +36,7 @@ int read_cfg(const char *fname, struct cfg *cfg)
{
FILE *fp;
char buf[512];
struct flock flk;
default_cfg(cfg);
@ -43,6 +45,12 @@ int read_cfg(const char *fname, struct cfg *cfg)
return -1;
}
/* aquire shared read lock */
flk.l_type = F_RDLCK;
flk.l_start = flk.l_len = 0;
flk.l_whence = SEEK_SET;
while(fcntl(fileno(fp), F_SETLKW, &flk) == -1);
while(fgets(buf, sizeof buf, fp)) {
int isnum;
char *key_str, *val_str, *line = buf;
@ -104,6 +112,12 @@ int read_cfg(const char *fname, struct cfg *cfg)
}
}
/* unlock */
flk.l_type = F_UNLCK;
flk.l_start = flk.l_len = 0;
flk.l_whence = SEEK_SET;
fcntl(fileno(fp), F_SETLK, &flk);
fclose(fp);
return 0;
}
@ -111,12 +125,19 @@ int read_cfg(const char *fname, struct cfg *cfg)
int write_cfg(const char *fname, struct cfg *cfg)
{
FILE *fp;
struct flock flk;
if(!(fp = fopen(fname, "w"))) {
fprintf(stderr, "failed to write config file %s: %s\n", fname, strerror(errno));
return -1;
}
/* aquire exclusive write lock */
flk.l_type = F_WRLCK;
flk.l_start = flk.l_len = 0;
flk.l_whence = SEEK_SET;
while(fcntl(fileno(fp), F_SETLKW, &flk) == -1);
fprintf(fp, "# sensitivity is multiplied with every motion (1.0 normal).\n");
fprintf(fp, "sensitivity = %.3f\n\n", cfg->sensitivity);
@ -140,6 +161,13 @@ int write_cfg(const char *fname, struct cfg *cfg)
if(cfg->invert[5]) fputc('z', fp);
fputs("\n\n", fp);
}
/* unlock */
flk.l_type = F_UNLCK;
flk.l_start = flk.l_len = 0;
flk.l_whence = SEEK_SET;
fcntl(fileno(fp), F_SETLK, &flk);
fclose(fp);
return 0;
}

Wyświetl plik

@ -18,7 +18,16 @@ start)
stop)
echo 'Stopping spacenavd daemon'
kill `pidof spacenavd`
# detect daemon's process id
pid=`cat /tmp/.spnavd.pid 2>/dev/null`
if [ $? != 0 ]; then
pid=`ps -e | grep spacenavd | awk '{ print $1 }'`
if [ -z "$pid" ]; then
echo 'spacenavd daemon is not running, nothing to do.'
exit 1
fi
fi
kill $pid
;;
reload|restart|force-reload)

Wyświetl plik

@ -100,7 +100,7 @@ void sig_handler(int s);
unsigned int msec_dif(struct timeval tv1, struct timeval tv2);
int dev_fd;
int dev_fd = -1;
char dev_name[128];
unsigned char evtype_mask[(EV_MAX + 7) / 8];
#define TEST_BIT(b, ar) (ar[b / 8] & (1 << (b % 8)))
@ -183,10 +183,7 @@ int main(int argc, char **argv)
signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler);
/* initialize the input device and the X11 connection (if available) */
if(init_dev() == -1) {
return 1;
}
init_dev();
init_unix();
#ifdef USE_X11
init_x11();
@ -862,11 +859,15 @@ int open_dev(const char *path)
if(ioctl(dev_fd, EVIOCGBIT(0, sizeof(evtype_mask)), evtype_mask) == -1) {
perror("EVIOCGBIT ioctl failed\n");
close(dev_fd);
dev_fd = -1;
return -1;
}
if(!TEST_BIT(EV_REL, evtype_mask)) {
fprintf(stderr, "Wrong device, no relative events reported!\n");
close(dev_fd);
dev_fd = -1;
return -1;
}
@ -1030,6 +1031,7 @@ void sig_handler(int s)
case SIGTERM:
close_x11(); /* call to avoid leaving garbage in the X server's root windows */
close_dev();
remove("/tmp/.spnavd.pid");
exit(0);
#ifdef USE_X11

Wyświetl plik

@ -32,7 +32,7 @@ else
fi
# detect daemon's process id
pid=`cat /tmp/.spnav.pid 2>/dev/null`
pid=`cat /tmp/.spnavd.pid 2>/dev/null`
if [ $? != 0 ]; then
pid=`ps -e | grep spacenavd | awk '{ print $1 }'`
if [ -z "$pid" ]; then