files.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 #include <errno.h>
00024 #include <stdlib.h>
00025 
00026 #ifdef HAVE_STRING_H
00027 #include <string.h>
00028 #endif
00029 
00030 #ifdef HAVE_STRINGS_H
00031 #include <strings.h>
00032 #endif
00033 
00034 #ifdef HAVE_UNISTD_H
00035 #include <unistd.h>
00036 #endif
00037 
00038 #ifdef HAVE_SYS_TYPES_H
00039 #include <sys/types.h>
00040 #endif
00041 
00042 #ifdef HAVE_SYS_STAT_H
00043 #include <sys/stat.h>
00044 #endif
00045 
00046 #include "monitor.h"
00047 
00048 
00049 /* Private variables */
00050 static time_t rc_last_modified;
00051 
00052 
00064 /* ------------------------------------------------------------------ Public */
00065 
00066 
00070 void init_files() {
00071 
00072   char pidfile[STRLEN];
00073 
00074   /* Set the location of this programs pidfile */
00075   if( !geteuid() ) {
00076     
00077     snprintf(pidfile, STRLEN, "%s/%s", MYPIDDIR, MYPIDFILE);
00078     
00079   } else {
00080     
00081     snprintf(pidfile, STRLEN, "%s/.%s", Run.Env.home, MYPIDFILE);
00082     
00083   }
00084   
00085   Run.pidfile= xstrdup(pidfile);
00086 
00087   set_file_timestamp();
00088   
00089 }
00090 
00091 
00095 void finalize_files() {
00096   
00097   unlink(Run.pidfile);
00098 
00099 }
00100 
00101 
00105 void re_init_files() {
00106 
00107   set_file_timestamp();
00108 
00109 }
00110 
00111 
00115 void set_file_timestamp() {
00116   
00117   struct stat buf;
00118 
00119   if ( !stat(Run.controlfile, &buf) ) {
00120     
00121     rc_last_modified= buf.st_mtime;
00122     
00123   }
00124 
00125 }
00126  
00127 
00134 char* find_rcfile() {
00135 
00136   char *rcfile= xmalloc(STRLEN);
00137   
00138   snprintf(rcfile, STRLEN, "%s/.%s", Run.Env.home, MONITRC);
00139   
00140   if ( exist_file(rcfile) ) {
00141     
00142     return (rcfile);
00143     
00144   }
00145 
00146   if ( exist_file(MONITRC) ) {
00147     
00148     memset(rcfile, 0, STRLEN);
00149     snprintf(rcfile, STRLEN, "%s/%s", Run.Env.cwd, MONITRC);
00150     
00151     return (rcfile);
00152     
00153   }
00154   
00155   memset(rcfile, 0, STRLEN);
00156   snprintf(rcfile, STRLEN, "/etc/%s", MONITRC);
00157   
00158   if ( exist_file(rcfile) ) 
00159     return (rcfile);
00160   
00161   error("%s: Cannot find the control file at ~/.%s, ./%s or at /etc/%s\n",
00162     prog, MONITRC, MONITRC, MONITRC);
00163   
00164   exit(1);
00165   
00166 }
00167 
00168 
00175 int create_pidfile(char *pidfile) {
00176   
00177   FILE *F= NULL;
00178 
00179   umask(PIDMASK);
00180   
00181   if ((F= fopen(pidfile,"w")) == (FILE *)NULL) {
00182     
00183     error("%s: Error opening pidfile '%s' for writing -- %s\n",
00184       prog, pidfile, STRERROR);
00185     
00186     return(FALSE);
00187     
00188   }
00189   
00190   fprintf(F, "%d", (int)getpid());
00191   fclose(F);
00192 
00193   return TRUE;
00194   
00195 }
00196 
00197 
00203 int is_rcfile_changed() {
00204   
00205   struct stat buf;
00206 
00207   if ( !stat(Run.controlfile, &buf) ) {
00208     
00209     return (buf.st_mtime != rc_last_modified);
00210     
00211   }
00212 
00213   return FALSE;
00214   
00215 }
00216 
00217 
00225 time_t file_changedtime(char *file) {
00226   
00227   struct stat buf;
00228 
00229   if(!stat(file, &buf))
00230       if(S_ISREG(buf.st_mode))
00231       return MAXIMUM(buf.st_mtime, buf.st_ctime);
00232   
00233   return FALSE;
00234 
00235 }
00236 
00237 
00246 int check_rcfile(char *rcfile) {
00247 
00248   struct stat buf;
00249   errno= 0;
00250 
00251   if(lstat(rcfile, &buf) < 0) {
00252     
00253     error("%s: Cannot stat the control file '%s' -- %s\n",
00254       prog, rcfile, STRERROR);
00255 
00256     return FALSE;
00257     
00258   }
00259     
00260   if(S_ISLNK(buf.st_mode)) {
00261     
00262     error("%s: The control file '%s' must not be a symbolic link.\n",
00263       prog, rcfile);
00264     
00265     return(FALSE);
00266     
00267   }
00268 
00269   if(!S_ISREG(buf.st_mode)) {
00270     
00271     error("%s: The control file '%s' is not a regular file.\n", prog, rcfile);
00272     
00273     return FALSE;
00274 
00275   }
00276 
00277   if(buf.st_uid != geteuid())  {
00278     
00279     error("%s: The control file '%s' must be owned by you.\n", prog, rcfile);
00280       
00281     return FALSE;
00282     
00283   }
00284 
00285   if(buf.st_mode & ~(S_IFREG | S_IREAD | S_IWRITE | S_IEXEC)) {
00286     
00287     error("%s: The control file '%s' must have no more than -rwx------ "
00288       "(0700) permissions.\n", prog, rcfile);
00289     
00290     return FALSE;
00291     
00292   }
00293 
00294   return TRUE;
00295 
00296 }
00297 
00298 
00304 int isreg_file(char *file) {
00305   
00306   struct stat buf;
00307   
00308   return (stat(file, &buf) == 0 && S_ISREG(buf.st_mode));
00309   
00310 }
00311 
00312 
00318 int exist_file(char *file) {
00319   
00320   struct stat buf;
00321   
00322   return (stat(file, &buf) == 0);
00323   
00324 }