Icinga-core 1.4.0
next gen monitoring
module/idoutils/src/sockdebug.c
Go to the documentation of this file.
00001 /***************************************************************
00002  * SOCKDEBUG.C - Socket Debugging Utility
00003  *
00004  * Copyright (c) 2005-2007 Ethan Galstad 
00005  * Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org)
00006  *
00007  **************************************************************/
00008 
00009 #include "../../../include/config.h"
00010 #include "../include/io.h"
00011 
00012 #define SOCKDEBUG_VERSION "1.4.0"
00013 #define SOCKDEBUG_NAME "SOCKDEBUG"
00014 #define SOCKDEBUG_DATE "05-11-2011"
00015 
00016 
00017 int cleanup_socket(int,char *);
00018 void sighandler(int);
00019 
00020 int sd=0;
00021 char *socketname=NULL;
00022 
00023 int main(int argc, char **argv){
00024         int new_sd=0;
00025 #ifdef HANDLE_MULTI
00026         int pid=0;
00027 #endif
00028         struct sockaddr_un server_address;
00029         struct sockaddr_un client_address;
00030         socklen_t client_address_length;
00031         char buf[1];
00032 
00033 
00034         if(argc<2){
00035                 printf("%s %s\n",SOCKDEBUG_NAME,SOCKDEBUG_VERSION);
00036                 printf("Copyright(c) 2009-2011 Icinga Development Team (http://www.icinga.org)\n");
00037                 printf("Copyright(c) 2005-2007 Ethan Galstad\n");
00038                 printf("Last Modified: %s\n",SOCKDEBUG_DATE);
00039                 printf("License: GPL v2\n");
00040                 printf("\n");
00041                 printf("Usage: %s <socketname>\n",argv[0]);
00042                 printf("\n");
00043                 printf("Creates a UNIX domain socket with name <socketname>, waits for a\n");
00044                 printf("client to connect and then prints all received data to stdout.\n");
00045                 printf("Only one client connection is processed at any given time.\n");
00046                 exit(1);
00047                 }
00048 
00049         /* initialize signal handling */
00050         signal(SIGQUIT,sighandler);
00051         signal(SIGTERM,sighandler);
00052         signal(SIGINT,sighandler);
00053 
00054         socketname=strdup(argv[1]);
00055         if(socketname==NULL){
00056                 perror("Could not dup socket name");
00057                 exit(1);
00058                  }
00059 
00060         /* create a socket */
00061         if(!(sd=socket(AF_UNIX,SOCK_STREAM,0))){
00062                 perror("Cannot create socket");
00063                 exit(1);
00064                 }
00065 
00066         /* bind the socket */
00067         strncpy(server_address.sun_path,socketname,sizeof(server_address.sun_path)); 
00068         server_address.sun_family=AF_UNIX; 
00069         if((bind(sd,(struct sockaddr *)&server_address,SUN_LEN(&server_address)))){
00070                 perror("Could not bind socket");
00071                 exit(1);
00072                 }
00073 
00074         /* listen for connections */
00075         if((listen(sd,1))){
00076                 perror("Cannot listen on socket");
00077                 cleanup_socket(sd,socketname);
00078                 exit(1);
00079                 }
00080 
00081         client_address_length=(socklen_t)sizeof(client_address);
00082 
00083         /* accept connections... */
00084         while(1){
00085 
00086                 if((new_sd=accept(sd,(struct sockaddr *)&client_address,(socklen_t *)&client_address_length))<0){
00087                         perror("Accept error");
00088                         cleanup_socket(sd,socketname);
00089                         exit(1);
00090                         }
00091 
00092 #ifdef HANDLE_MULTI
00093                 /* fork... */
00094                 pid=fork();
00095 
00096                 switch(pid){
00097                 case -1:
00098                         perror("Fork error");
00099                         cleanup_socket(sd,socketname);
00100                         exit(1);
00101                         break;
00102 
00103                 case 0:
00104                         /* print all data from socket to the screen */
00105                         while((read(new_sd,buf,sizeof(buf)))){
00106                                 printf("%c",buf[0]);
00107                                 }
00108                         exit(0);
00109                         break;
00110 
00111                 default:
00112                         close(new_sd);
00113                         break;
00114                         }
00115 
00116 #else
00117                 /* print all data from socket to the screen */
00118                 while((read(new_sd,buf,sizeof(buf)))){
00119                         printf("%c",buf[0]);
00120                         }
00121 
00122                 close(new_sd);
00123 #endif
00124                 
00125                 }
00126 
00127         /* cleanup after ourselves */
00128         cleanup_socket(sd,socketname);
00129 
00130         return 0;
00131         }
00132 
00133 
00134 int cleanup_socket(int s, char *f){
00135 
00136         /* close the socket */
00137         shutdown(s,2);
00138         close(s);
00139 
00140         /* unlink the file */
00141         unlink(f);
00142 
00143         return 0;
00144         }
00145 
00146 
00147 
00148 void sighandler(int sig){
00149 
00150         /* close the socket */
00151         shutdown(sd,2);
00152         close(sd);
00153 
00154         /* unlink the file */
00155         unlink(socketname);
00156 
00157         exit(0);
00158 
00159         return;
00160         }
 All Data Structures Files Functions Variables Typedefs Defines