monit_http.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C), 2000-2002 by Contributors to the monit codebase. 
00003  * All Rights Reserved.
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include <stdio.h>
00023 
00024 #ifdef HAVE_SYS_TYPES_H
00025 #include <sys/types.h>
00026 #endif
00027 
00028 #include <sys/socket.h>
00029 #include <errno.h>
00030 #include <stdlib.h>
00031 
00032 #ifdef HAVE_STRING_H
00033 #include <string.h>
00034 #endif
00035 
00036 #ifdef HAVE_UNISTD_H
00037 #include <unistd.h>
00038 #endif
00039 
00040 #ifdef HAVE_SYS_WAIT_H
00041 #include <sys/wait.h>
00042 #endif
00043 
00044 #include "monitor.h"
00045 #include "net.h"
00046 #include "engine.h"
00047 
00048 /* Private prototypes */
00049 static void *thread_wrapper(void *arg);
00050 
00051 /* The HTTP Thread */
00052 static pthread_t thread;
00053 
00054 
00067 /* ------------------------------------------------------------------ Public */
00068 
00069 
00073 int check_httpd() {
00074 
00075   return check_connect(Run.bind_addr?Run.bind_addr:"localhost",
00076                Run.httpdport, SOCK_STREAM);
00077 
00078 }
00079 
00080 
00086 int can_http() {
00087 
00088   if ( Run.dohttpd && Run.isdaemon ) {
00089     
00090     if ( ! has_hosts_allow() && ! Run.Auth.defined ) {
00091       
00092       error("%s: monit httpd not started since no connect allowed\n",
00093         prog);
00094       
00095       return FALSE;
00096       
00097     }
00098     
00099     return TRUE;
00100     
00101   }
00102   
00103   return FALSE;
00104   
00105 }
00106 
00107 
00112 void monit_http(int action) {
00113 
00114   int status;
00115 
00116   switch ( action ) {
00117     
00118   case STOP_HTTP: {
00119     
00120        log("Stopping %s HTTP server\n", prog);
00121        stop_httpd();
00122        
00123       }
00124       
00125       return;
00126       
00127   case START_HTTP:
00128       
00129       log("Starting %s HTTP server at portnumber %d\n", prog, Run.httpdport);
00130       
00131       break;
00132       
00133   }
00134 
00135   status= pthread_create(&thread, NULL, thread_wrapper, NULL);
00136   
00137   if ( status != 0 ) {
00138     
00139     log("%s: Failed to create the http server. Thread error -- %s.\n",
00140     prog, strerror(status));
00141     
00142   }
00143   
00144 }
00145 
00146 
00147 /* ----------------------------------------------------------------- Private */
00148 
00149 
00150 static void *thread_wrapper(void *arg) {
00151 
00152   sigset_t ns;
00153 
00154   pthread_detach(pthread_self());
00155   
00156   /* Block the SIGUSR1 signal in this thread */
00157   sigemptyset(&ns);
00158   sigaddset(&ns, SIGUSR1);
00159   pthread_sigmask(SIG_BLOCK, &ns, NULL);
00160 
00161   start_httpd(Run.httpdport, 10, Run.bind_addr);
00162 
00163   return NULL;
00164 
00165 }
00166 
00167