start.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 start() {
00057 
00058   Process_T p;
00059  
00060   for(p= processlist; p; p= p->next) {
00061     
00062     if(p->start) dstart_process(p->name);
00063     
00064   }
00065   
00066 }
00067 
00068 
00072 void start_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->start) dstart_process(p->name);
00081 
00082     }
00083     
00084     
00085   }
00086 
00087 }
00088   
00089 
00094 void start_process(Process_T p) {
00095   
00096   pid_t  pid= -1;
00097 
00098   if(!p->start) {
00099 
00100     error("Start: '%s' the start program is not defined\n", p->name);
00101     return;
00102 
00103   }
00104 
00105   if((pid= is_process_running(p))) {
00106     
00107     error("Start: '%s' is already started with pid=%d\n",
00108       p->name, (int)pid);
00109     
00110     return;
00111     
00112   }
00113 
00114   log("Start: (%s) %s\n", p->name, p->start->arg[0]);
00115 
00116   spawn(p, p->start);
00117   
00118 }
00119 
00120 
00125 void dstart_process(char *P) {
00126 
00127   int pid;
00128   
00129   if ( ! exist_process(P) ) {
00130     
00131     error("%s: Cannot start program '%s' -- not found in %s\n",
00132       prog, P, Run.controlfile);
00133     
00134     return;
00135     
00136   }
00137   
00138   if ( (pid= is_process_running(get_process(P))) ) {
00139     
00140     error("%s: '%s' is already running with pid=%d\n", prog, P, pid);
00141     
00142     return;
00143     
00144   }
00145   
00146   if ( exist_daemon() ) {
00147     
00148     /* If a monit daemon exist we request that the daemon start the
00149        program in case the daemon timed out the program. This way any
00150        process timeout lock in the daemon is removed */
00151     
00152     int s= create_socket(Run.bind_addr?Run.bind_addr:"localhost",
00153              Run.httpdport, SOCK_STREAM);
00154     
00155     if (s<0) {
00156       
00157       error("%s: Cannot connect to the monit daemon. "
00158         "Did you start it with http support?\n", prog);
00159       
00160       return;
00161       
00162     } else {
00163 
00164       char req[2*STRLEN];
00165       char *auth= get_basic_authentication_header();
00166       
00167       snprintf(req, 2*STRLEN,
00168            "GET /%s?action=start HTTP/1.0\r\n%s\r\n", P, auth);
00169       
00170       sock_send(s, req, sizeof(req), 0);
00171       close_socket(s);
00172       free(auth);
00173       
00174     }
00175   } else {
00176     
00177     /* No monit daemon exist, just start the program */
00178     start_process(get_process(P));
00179     
00180   }
00181   
00182 }