stop.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 <stdlib.h>
00030 
00031 #ifdef HAVE_STRING_H
00032 #include <string.h>
00033 #endif
00034 
00035 #include "monitor.h"
00036 #include "net.h"
00037 
00038 
00050 /* ------------------------------------------------------------------ Public */
00051 
00052 
00056 void stop() {
00057 
00058   Process_T p;
00059   
00060   for(p= processlist; p; p= p->next) {
00061     
00062     if(p->stop) dstop_process(p->name);
00063     
00064   }
00065   
00066 }
00067 
00068 
00072 void stop_group(char *G) {
00073 
00074   Process_T p;
00075  
00076   for(p= processlist; p; p= p->next) {
00077 
00078     if(is(p->group, G)) {
00079 
00080       if(p->stop) dstop_process(p->name);
00081       
00082     }
00083     
00084   }
00085 
00086 }
00087   
00088 
00093 void stop_process(Process_T p) {
00094   
00095   pid_t  pid= -1;
00096   
00097   if(!p->stop) {
00098 
00099     error("Stop: '%s' the stop program is not defined\n", p->name);
00100     return;
00101     
00102   }
00103   
00104   if(!(pid= is_process_running(p))) {
00105     
00106     error("Stop: '%s' is not running\n", p->name);
00107     
00108     return;
00109     
00110   }
00111   
00112   log("Stop: (%s) %s\n", p->name, p->stop->arg[0]);
00113   
00114   spawn(p, p->stop);
00115   
00116 }
00117 
00118 
00123 void dstop_process(char *P) {
00124 
00125   if ( ! exist_process(P) ) {
00126     
00127     error("%s: Cannot stop program '%s' -- not found in %s\n",
00128       prog, P, Run.controlfile);
00129 
00130     return;
00131     
00132   }
00133   
00134   if ( ! is_process_running(get_process(P)) ) {
00135     
00136     error("%s: '%s' is not running\n", prog, P);
00137     
00138     return;
00139     
00140   }
00141   
00142   if ( exist_daemon() ) {
00143     
00144     /* If a monit daemon exist we request that the daemon stop the
00145        program, if not, it will start the progam again on it's next
00146        cycle */
00147     
00148     int s= create_socket(Run.bind_addr?Run.bind_addr:"localhost",
00149              Run.httpdport, SOCK_STREAM);
00150     if (s<0) {
00151       
00152       error("%s: Cannot connect to the monit daemon. "
00153         "Did you start it with http support?\n", prog);
00154       
00155       return;
00156       
00157     } else {
00158       
00159       char req[2*STRLEN];
00160       char *auth= get_basic_authentication_header();
00161 
00162       snprintf(req, 2*STRLEN,
00163            "GET /%s?action=stop HTTP/1.0\r\n%s\r\n", P, auth);
00164       
00165       sock_send(s, req, sizeof(req), 0);
00166       close_socket(s);
00167       free(auth);
00168 
00169     }
00170     
00171   } else {
00172     
00173     /* No monit daemon exist, just stop the program */
00174     stop_process(get_process(P));
00175     
00176   }
00177 
00178 }