[tuxdroid-svn] r251 - svnlook: warning: cannot set LC_CTYPE locale svnlook: warning: environment va
Status: Beta
Brought to you by:
ks156
From: svnlook:warning@affinitic.be:cannot s. L. l. <c2m...@c2...> - 2007-04-13 23:31:57
|
Author: svnlook: warning: cannot set LC_CTYPE locale Date: svnlook: warning: environment variable LANG is EN New Revision: 251 Added: daemon/trunk/libs/USBDaemon_pidfile.c daemon/trunk/libs/USBDaemon_pidfile.h Modified: daemon/trunk/Makefile daemon/trunk/libs/USBDaemon_command_tux.c daemon/trunk/libs/USBDaemon_globals.h daemon/trunk/libs/USBDaemon_tcp_server.c daemon/trunk/libs/USBDaemon_usb_enum.c daemon/trunk/main.c Log: doegox 2007-04-14 01:31:48 +0200 (Sat, 14 Apr 2007) 278 More adequate unicity management through pid file stored in /var/run. Note that apart from the fact that relying on external progs (ps, grep, wc) is not very nice, the previous "ps ax..." method was failing as sometimes ps sees the very new tuxdaemon process and sometimes not. svnlook: warning: cannot set LC_CTYPE locale svnlook: warning: environment variable LANG is EN svnlook: warning: please check that your locale name is correct Modified: daemon/trunk/Makefile =================================================================== --- daemon/trunk/Makefile 2007-04-13 19:08:35 UTC (rev 250) +++ daemon/trunk/Makefile 2007-04-13 23:31:48 UTC (rev 251) @@ -41,6 +41,7 @@ $(OBJ_DIR)/USBDaemon_usb_enum.o \ $(OBJ_DIR)/USBDaemon_usb_readWrite.o \ $(OBJ_DIR)/USBDaemon_status_table.o \ + $(OBJ_DIR)/USBDaemon_pidfile.o \ $(OBJ_DIR)/USBDaemon_command_tux.o \ $(OBJ_DIR)/USBDaemon_tcp_server.o @@ -87,6 +88,7 @@ libs/USBDaemon_usb_enum.h \ libs/USBDaemon_tcp_server.h \ libs/USBDaemon_status_table.h \ +libs/USBDaemon_pidfile.h \ tuxdefs/commands.h \ version.h $(compile_source) @@ -95,6 +97,7 @@ libs/USBDaemon_usb_enum.h \ libs/USBDaemon_globals.h \ libs/USBDaemon_usb_readWrite.h \ +libs/USBDaemon_pidfile.h \ libs/USBDaemon_command_tux.h $(compile_source) @@ -112,12 +115,18 @@ libs/USBDaemon_tcp_server.h $(compile_source) +$(OBJ_DIR)/USBDaemon_pidfile.o: libs/USBDaemon_pidfile.c \ +libs/USBDaemon_globals.h \ +libs/USBDaemon_pidfile.h + $(compile_source) + $(OBJ_DIR)/USBDaemon_command_tux.o: libs/USBDaemon_command_tux.c \ libs/USBDaemon_status_table.h \ tuxdefs/commands.h \ libs/USBDaemon_command_tux.h \ libs/USBDaemon_usb_readWrite.h \ libs/USBDaemon_globals.h \ +libs/USBDaemon_pidfile.h \ libs/USBDaemon_tcp_server.h $(compile_source) @@ -126,6 +135,7 @@ libs/USBDaemon_command_tux.h \ libs/USBDaemon_status_table.h \ tuxdefs/commands.h \ +libs/USBDaemon_pidfile.h \ libs/USBDaemon_tcp_server.h $(compile_source) Modified: daemon/trunk/libs/USBDaemon_command_tux.c =================================================================== --- daemon/trunk/libs/USBDaemon_command_tux.c 2007-04-13 19:08:35 UTC (rev 250) +++ daemon/trunk/libs/USBDaemon_command_tux.c 2007-04-13 23:31:48 UTC (rev 251) @@ -28,6 +28,7 @@ #include "USBDaemon_globals.h" #include "../tuxdefs/commands.h" #include "USBDaemon_tcp_server.h" +#include "USBDaemon_pidfile.h" /*_____________________ F U N C T I O N S __________________________________*/ /************************************************************************** */ @@ -255,7 +256,7 @@ printf("WARNING : daemon killed by user\n"); send_daemon_disconnected(); result[0] = ACK_CMD_OK; - exit(0); + terminate(0); break; case SUB_D_CMD_STRUC_DEFINE_CLIENT_NAME: { Modified: daemon/trunk/libs/USBDaemon_globals.h =================================================================== --- daemon/trunk/libs/USBDaemon_globals.h 2007-04-13 19:08:35 UTC (rev 250) +++ daemon/trunk/libs/USBDaemon_globals.h 2007-04-13 23:31:48 UTC (rev 251) @@ -33,6 +33,8 @@ #define TUX_WRITE_TIMEOUT 5000 #define TUX_READ_TIMEOUT 5000 #define LIBUSB_DEBUG_LEVEL 1 +#define NEWUID 65534 +#define NEWGID 65534 /*_____________________ G L O B A L S _ V A R I A B L E S _______________________*/ extern unsigned char show_frames; extern unsigned char show_raw_status; Added: daemon/trunk/libs/USBDaemon_pidfile.c =================================================================== --- daemon/trunk/libs/USBDaemon_pidfile.c (rev 0) +++ daemon/trunk/libs/USBDaemon_pidfile.c 2007-04-13 23:31:48 UTC (rev 251) @@ -0,0 +1,146 @@ +/* +* Tux Droid - USB Daemon +* Copyright (C) 2007 C2ME Sa <rem...@c2...> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2, or (at your option) +* any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +* 02111-1307, USA. +*/ +/* $Id: main.c 242 2007-04-13 08:48:33Z remi $ */ + +/*_____________________ I N C L U D E S____________________________________ */ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/stat.h> +#include <sys/file.h> +#include <string.h> +#include <errno.h> +#include <signal.h> +#include "USBDaemon_globals.h" + +#define PidFile "/var/run/tuxdaemon.pid" + +/************************************************************************ */ +/* Functions to deal with the pidfile + Their original version come from the sysklogd package, under GPL + Copyright (c) 1995 Martin Schulze <Mar...@Li...> */ +/************************************************************************ */ + +/* read_pid + * + * Reads the specified pidfile and returns the read pid. + * 0 is returned if either there's no pidfile, it's empty + * or no pid can be read. + */ +int read_pid (void) +{ + FILE *f; + int pid; + + if (!(f=fopen(PidFile,"r"))) + return 0; + fscanf(f,"%d", &pid); + fclose(f); + return pid; +} + +/* check_pid + * + * Reads the pid using read_pid and looks up the pid in the process + * table (using /proc) to determine if the process already exists. If + * so 1 is returned, otherwise 0. + */ +int check_pid (void) +{ + int pid = read_pid(); + + /* Amazing ! _I_ am already holding the pid file... */ + if ((!pid) || (pid == getpid ())) + return 0; + + /* + * The 'standard' method of doing this is to try and do a 'fake' kill + * of the process. If an ESRCH error is returned the process cannot + * be found -- GW + */ + /* But... errno is usually changed only on error.. */ + if (kill(pid, 0) && errno == ESRCH) + return(0); + + return pid; +} + +/* write_pid + * + * Writes the pid to the specified file. If that fails 0 is + * returned, otherwise the pid. + */ +int write_pid (void) +{ + FILE *f; + int fd; + int pid; + + if ( ((fd = open(PidFile, O_RDWR|O_CREAT|O_TRUNC, 0644)) == -1) + || ((f = fdopen(fd, "r+")) == NULL) ) { + fprintf(stderr, "Can't open or create %s.\n", PidFile); + return 0; + } + + if (flock(fd, LOCK_EX|LOCK_NB) == -1) { + fscanf(f, "%d", &pid); + fclose(f); + fprintf(stderr, "Can't lock, lock is held by pid %d.\n", pid); + return 0; + } + + pid = getpid(); + if (!fprintf(f,"%d\n", pid)) { + fprintf(stderr, "Can't write pid , %s.\n", strerror(errno)); + close(fd); + return 0; + } + fflush(f); + + if (flock(fd, LOCK_UN) == -1) { + fprintf(stderr, "Can't unlock pidfile %s, %s.\n", PidFile, strerror(errno)); + close(fd); + return 0; + } + close(fd); + chown(PidFile, NEWUID, NEWGID); + return pid; +} + +/* remove_pid + * + * Remove the the specified file. The result from unlink(2) + * is returned + */ +int remove_pid (void) +{ + return unlink (PidFile); +} + +/************************************************************************ */ +/* Terminate */ +/************************************************************************ */ +void terminate(int ret) +{ + if (remove_pid()) { + fprintf(stderr, "Could not delete PID file\n"); + } + exit(ret); +} Added: daemon/trunk/libs/USBDaemon_pidfile.h =================================================================== --- daemon/trunk/libs/USBDaemon_pidfile.h (rev 0) +++ daemon/trunk/libs/USBDaemon_pidfile.h 2007-04-13 23:31:48 UTC (rev 251) @@ -0,0 +1,62 @@ +/* +* Tux Droid - USB Daemon +* Copyright (C) 2007 C2ME Sa <rem...@c2...> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2, or (at your option) +* any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +* 02111-1307, USA. +*/ +/* $Id: main.c 242 2007-04-13 08:48:33Z remi $ */ + +/************************************************************************ */ +/* Functions to deal with the pidfile + Their original version come from the sysklogd package, under GPL + Copyright (c) 1995 Martin Schulze <Mar...@Li...> */ +/************************************************************************ */ + +/* read_pid + * + * Reads the specified pidfile and returns the read pid. + * 0 is returned if either there's no pidfile, it's empty + * or no pid can be read. + */ +extern int read_pid (void); + +/* check_pid + * + * Reads the pid using read_pid and looks up the pid in the process + * table (using /proc) to determine if the process already exists. If + * so 1 is returned, otherwise 0. + */ +extern int check_pid (void); + +/* write_pid + * + * Writes the pid to the specified file. If that fails 0 is + * returned, otherwise the pid. + */ +extern int write_pid (void); + +/* remove_pid + * + * Remove the the specified file. The result from unlink(2) + * is returned + */ +extern int remove_pid (void); + +/* terminate + * + * Remove the pid file and exit + */ +extern void terminate(int ret); Modified: daemon/trunk/libs/USBDaemon_tcp_server.c =================================================================== --- daemon/trunk/libs/USBDaemon_tcp_server.c 2007-04-13 19:08:35 UTC (rev 250) +++ daemon/trunk/libs/USBDaemon_tcp_server.c 2007-04-13 23:31:48 UTC (rev 251) @@ -31,6 +31,7 @@ #include "USBDaemon_command_tux.h" #include "USBDaemon_status_table.h" #include "USBDaemon_tcp_server.h" +#include "USBDaemon_pidfile.h" /*_____________________ V A R I A B L E S __________________________________*/ int tcp_server_handle = -1; int tcp_clients_handle[TUX_MAX_TCP_CLIENTS] = @@ -79,7 +80,7 @@ if (bind(tcp_server_handle, (struct sockaddr *) &tcp_server_sockaddr, sizeof(tcp_server_sockaddr)) < 0) { printf("TCP socket : binding error\n"); - exit(2); + terminate(2); } if (listen(tcp_server_handle, 5) < 0) { Modified: daemon/trunk/libs/USBDaemon_usb_enum.c =================================================================== --- daemon/trunk/libs/USBDaemon_usb_enum.c 2007-04-13 19:08:35 UTC (rev 250) +++ daemon/trunk/libs/USBDaemon_usb_enum.c 2007-04-13 23:31:48 UTC (rev 251) @@ -28,6 +28,7 @@ #include "USBDaemon_usb_readWrite.h" #include "USBDaemon_command_tux.h" #include "USBDaemon_tcp_server.h" +#include "USBDaemon_pidfile.h" /*_____________________ V A R I A B L E S __________________________________*/ usb_dev_handle *tux_handle; struct usb_device* tux_device; @@ -71,8 +72,8 @@ /* Open usb device */ tux_hdl = usb_open(tux_dev); - setgid(65534); - setuid(65534); + setgid(NEWGID); + setuid(NEWUID); if (!tux_hdl) { printf("Can't open device: %s (%d)\n", strerror(errno), errno); @@ -120,7 +121,7 @@ "the firmware to version 1.00 or better.\n"\ "Check http://www.tuxisalive.com/documentation/how-to/updating-the-firmware\n"\ "for details.\n"); - exit(1); + terminate(1); } /* Get Device handle */ @@ -128,7 +129,7 @@ if(tux_handle == NULL) { printf("You must load the daemon in root mode\n"); - exit(1); + terminate(1); } /* Signals that root privileges were dropped */ @@ -151,7 +152,7 @@ { printf("Tux dongle is disconnected\n"); send_daemon_disconnected(); - exit(1); + terminate(1); } usb_get_status_TuxDroid(); usleep(50000); Modified: daemon/trunk/main.c =================================================================== --- daemon/trunk/main.c 2007-04-13 19:08:35 UTC (rev 250) +++ daemon/trunk/main.c 2007-04-13 23:31:48 UTC (rev 251) @@ -32,7 +32,9 @@ #include <fcntl.h> #include <time.h> #include <signal.h> +#include <sys/file.h> #include "libs/USBDaemon_globals.h" +#include "libs/USBDaemon_pidfile.h" #include "libs/USBDaemon_usb_enum.h" #include "libs/USBDaemon_tcp_server.h" #include "libs/USBDaemon_status_table.h" @@ -98,7 +100,7 @@ { send_daemon_disconnected(); printf("Daemon closed\n"); - exit(0); + terminate(0); } /************************************************************************ */ @@ -137,10 +139,10 @@ case 0: break; case -1: /*erreur */ - exit(1); + terminate(1); break; default : - exit(0); + terminate(0); } umask(0); @@ -148,17 +150,19 @@ if (sid < 0) { perror("sid"); - exit(1); + terminate(1); } if (chdir("/") < 0) { perror("chdir"); - exit(1); + terminate(1); } daemon_pid = (unsigned short)getpid(); printf("Tux USB Daemon started : pid = %d\n", (int)getpid()); + if (!write_pid()) + exit(1); /* No shell */ if (!shell_view) @@ -185,22 +189,6 @@ } /************************************************************************ */ -/* Check the number of instances */ -/************************************************************************ */ -unsigned char single_instance() -{ - FILE* tmp; - char c; - unsigned char count; - count = 0; - tmp = popen("ps -e -o'%y %c' | grep tuxdaemon | wc --line","r"); - fread(&c, sizeof(char), 1, tmp); - pclose(tmp); - if ((char)c == (char)'1') return 1; - else return 0; -} - -/************************************************************************ */ /* USBDaemon main function */ /************************************************************************ */ int main(int argc, char *argv[]) @@ -217,10 +205,9 @@ signal(SIGINT, on_close_daemon); signal(SIGKILL, on_close_daemon); /* Only one instance of the daemon is authorized */ - if (!single_instance()) - { + if (check_pid()) { printf("Tuxdaemon is already launched !\n"); - exit(0); + exit(0); } /* Program arguments */ for (i = 1; i < argc; i++) @@ -249,6 +236,9 @@ if (daemonized) daemonize(shell_view); + else + if (!write_pid()) + exit(1); /* Setup & print daemon information */ setup_daemon_info(); @@ -268,7 +258,7 @@ else { printf("g_thread NOT supported\n"); - return 1; + terminate(1); } pthread_cond_init(&dropcond, 0); @@ -278,7 +268,7 @@ if (UsbThread == NULL) { printf("Thread creation failed: %s \n", error->message); - return 2; + terminate(2); } /* Start TCP task thread */ @@ -287,11 +277,12 @@ if (TcpThread == NULL) { printf("Thread creation failed: %s \n", error->message); - return 2; + terminate(2); } loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(loop); - return 0; + terminate(0); + return(0); } |