[tuxdroid-svn] r148 - daemon/trunk
Status: Beta
Brought to you by:
ks156
From: jaguarondi <c2m...@c2...> - 2007-03-13 14:07:18
|
Author: jaguarondi Date: 2007-03-13 15:07:04 +0100 (Tue, 13 Mar 2007) New Revision: 148 Modified: daemon/trunk/main.c Log: - CLN: main.c was not committed last time, same patch from srikanta Modified: daemon/trunk/main.c =================================================================== --- daemon/trunk/main.c 2007-03-12 16:17:30 UTC (rev 147) +++ daemon/trunk/main.c 2007-03-13 14:07:04 UTC (rev 148) @@ -18,6 +18,7 @@ * 02111-1307, USA. */ /* $Id$ */ + /*_____________________ I N C L U D E S____________________________________ */ #include <stdio.h> #include <stdlib.h> @@ -41,186 +42,194 @@ struct version_s version = {0, 0, 0, "\0", "\0", 0}; - // *********************************************************************** - // Print_info_daemon - // *********************************************************************** - void - print_info_daemon() - { - const char delimiters[] = " .-"; - char cp[] = VERSION; - char *token; +// *********************************************************************** +// Print_info_daemon +// *********************************************************************** +void +print_info_daemon() +{ + const char delimiters[] = " .-"; + char cp[] = VERSION; + char *token; - if ((token = strtok (cp, delimiters)) != NULL) - version.major = atoi (token); - if ((token = strtok (NULL, delimiters)) != NULL) - version.minor = atoi (token); - if ((token = strtok (NULL, delimiters)) != NULL) - version.release = atoi (token); - if ((token = strtok (NULL, delimiters)) != NULL) - strncpy (version.maturity, token, 10); - if ((token = strtok (NULL, delimiters)) != NULL) - strncpy (version.release_status, token, 20); + if ((token = strtok (cp, delimiters)) != NULL) + version.major = atoi (token); + if ((token = strtok (NULL, delimiters)) != NULL) + version.minor = atoi (token); + if ((token = strtok (NULL, delimiters)) != NULL) + version.release = atoi (token); + if ((token = strtok (NULL, delimiters)) != NULL) + strncpy (version.maturity, token, 10); + if ((token = strtok (NULL, delimiters)) != NULL) + strncpy (version.release_status, token, 20); - printf("-----------------------------------\n"); - printf("Tux USB Daemon version %s\n",VERSION); - printf("Kysoh 2007.\n"); - printf("-----------------------------------\n"); - } + printf("-----------------------------------\n"); + printf("Tux USB Daemon version %s\n", VERSION); + printf("Kysoh 2007.\n"); + printf("-----------------------------------\n"); +} - // *********************************************************************** - // On_close_daemon - // *********************************************************************** - void - on_close_daemon() +// *********************************************************************** +// On_close_daemon +// *********************************************************************** +void +on_close_daemon() +{ + send_daemon_disconnected(); + printf("Daemon closed\n"); + exit(0); +} + +// *********************************************************************** +// Daemonizer +// *********************************************************************** +void +daemonize(unsigned char shell_view) +{ + pid_t pid, sid; + + pid = fork(); + switch (pid) { - send_daemon_disconnected(); - printf("Daemon closed\n"); - exit(0); + case 0: + break; + case -1: //erreur + exit(1); + break; + default : + exit(0); } - - // *********************************************************************** - // Daemonizer - // *********************************************************************** - void - daemonize(unsigned char shell_view) + umask(0); + sid = setsid(); + if(sid < 0) { - pid_t pid,sid; - - pid=fork(); - switch(pid){ - case 0: - break; - case -1: //erreur - exit(1); - break; - default : - exit(0); - } - umask(0); - sid=setsid(); - if(sid<0){ - perror("sid"); - exit(1); - } - if(chdir("/")<0){ - perror("chdir"); - exit(1); - } - DAEMON_PID=(unsigned short)getpid(); - printf("Daemon started : pid=%d\n",(int)getpid()); - // No shell - if(!shell_view) - { - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - } + perror("sid"); + exit(1); } - - // *********************************************************************** - // Show usages of tux daemon - // *********************************************************************** - void - usage() + if(chdir("/") < 0) { - printf("usage : tuxdaemon [options]\n"); - printf("options:\n"); - printf(" -d start in daemon mode\n"); - printf(" -v view in shell ( for daemon mode )\n"); - printf(" -st show the structure of the usb trames\n"); - printf(" -sr show all raw statutes\n"); - printf(" -si show only invalid raw statutes\n"); - printf(" -h show this usage list\n"); + perror("chdir"); + exit(1); } - - // *********************************************************************** - // USBDaemon main function - // *********************************************************************** - int - main( - int argc, - char *argv[] - ) + DAEMON_PID = (unsigned short)getpid(); + printf("Daemon started : pid = %d\n", (int)getpid()); + // No shell + if(!shell_view) { - GMainLoop *loop; - GThread *UsbThread; - GThread *TcpThread; - GError *error=NULL; - unsigned char i; - unsigned char shell_view; - unsigned char daemonized; - - // Print daemon informations - print_info_daemon(); + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + } +} +// *********************************************************************** +// Show usages of tux daemon +// *********************************************************************** +void +usage() +{ + printf("usage : tuxdaemon [options]\n"); + printf("options:\n"); + printf(" -d start in daemon mode\n"); + printf(" -v view in shell ( for daemon mode )\n"); + printf(" -st show the structure of the usb trames\n"); + printf(" -sr show all raw statutes\n"); + printf(" -si show only invalid raw statutes\n"); + printf(" -h show this usage list\n"); +} + +// *********************************************************************** +// USBDaemon main function +// *********************************************************************** +int +main(int argc, char *argv[]) +{ + GMainLoop *loop; + GThread *UsbThread; + GThread *TcpThread; + GError *error = NULL; + unsigned char i; + unsigned char shell_view; + unsigned char daemonized; + + // Print daemon informations + print_info_daemon(); + #ifdef USB_DEBUG - // Set libusb debug level - usb_set_debug(LIBUSB_DEBUG_LEVEL); - usb_init(); + // Set libusb debug level + usb_set_debug(LIBUSB_DEBUG_LEVEL); + usb_init(); #endif - // Program arguments - daemonized=0; - shell_view=0; - SHOW_TRAMES=0; - SHOW_RAW_STATUS=0; - SHOW_INVALID_RAW=0; - if(argc>1){ - for(i=1;i<argc;i++){ - if(!strcmp("-v", argv[i])) - shell_view=1; - else if(!strcmp("-d", argv[i])) - daemonized=1; - else if(!strcmp("-st", argv[i])) - SHOW_TRAMES=1; - else if(!strcmp("-sr", argv[i])) - SHOW_RAW_STATUS=1; - else if(!strcmp("-si", argv[i])) - SHOW_INVALID_RAW=1; - else if(!strcmp("-h", argv[i])){ - usage(); - exit(1); - }else{ - usage(); - exit(1); - } + // Program arguments + daemonized = 0; + shell_view = 0; + SHOW_TRAMES = 0; + SHOW_RAW_STATUS = 0; + SHOW_INVALID_RAW = 0; + if(argc > 1) + { + for (i = 1; i < argc; i++) + { + if(!strcmp("-v", argv[i])) + shell_view = 1; + else if(!strcmp("-d", argv[i])) + daemonized = 1; + else if(!strcmp("-st", argv[i])) + SHOW_TRAMES = 1; + else if(!strcmp("-sr", argv[i])) + SHOW_RAW_STATUS = 1; + else if(!strcmp("-si", argv[i])) + SHOW_INVALID_RAW = 1; + else if(!strcmp("-h", argv[i])) + { + usage(); + exit(1); } + else + { + usage(); + exit(1); + } } - if(daemonized) - daemonize(shell_view); - // On exiting - signal(SIGINT, on_close_daemon); - signal(SIGKILL, on_close_daemon); - // Gestion Multithread - if (!g_thread_supported()) { - g_thread_init(NULL); - }else{ - printf("g_thread NOT supported\n"); - return 1; - } - - TUX_USB_STATUS=0; - TUX_TCP_STATUS=0; - CLOSE_DAEMON=0; - last_remote_key=0xFF; - last_toggle_key=0xFF; - sound_flash_count=0; - - // Start USB task thread - if ((UsbThread = g_thread_create((GThreadFunc)usb_start_task,NULL,TRUE,&error))== NULL ) { - printf("Thread creation failed: %s \n",error->message); - return 2; - } - - // Start TCP task thread - if ((TcpThread = g_thread_create((GThreadFunc)tcp_server_start_task,NULL,TRUE,&error))== NULL ) { - printf("Thread creation failed: %s \n",error->message); - return 2; - } - loop = g_main_loop_new(NULL,FALSE); - g_main_loop_run(loop); - return 0; } + if(daemonized) + daemonize(shell_view); + // On exiting + signal(SIGINT, on_close_daemon); + signal(SIGKILL, on_close_daemon); + // Gestion Multithread + if (!g_thread_supported()) + { + g_thread_init(NULL); + } + else + { + printf("g_thread NOT supported\n"); + return 1; + } - + TUX_USB_STATUS = 0; + TUX_TCP_STATUS = 0; + CLOSE_DAEMON = 0; + last_remote_key = 0xFF; + last_toggle_key = 0xFF; + sound_flash_count = 0; + + // Start USB task thread + if ((UsbThread = g_thread_create((GThreadFunc)usb_start_task, NULL, TRUE, &error)) == NULL ) + { + printf("Thread creation failed: %s \n", error->message); + return 2; + } + + // Start TCP task thread + if ((TcpThread = g_thread_create((GThreadFunc)tcp_server_start_task, NULL, TRUE, &error)) == NULL ) + { + printf("Thread creation failed: %s \n", error->message); + return 2; + } + loop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(loop); + return 0; +} |