]> git.sur5r.net Git - openldap/blob - servers/slurpd/sanity.c
import localtime -> gmtime change from -devel
[openldap] / servers / slurpd / sanity.c
1 /*
2  * Copyright (c) 1996 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13
14 /*
15  * sanity.c - perform sanity checks on the environment at startup time,
16  * and report any errors before we disassociate from the controlling tty,
17  * start up our threads, and do other stuff which makes it hard to give
18  * feedback to the users.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include "slurp.h"
28 #include "globals.h"
29 #include "portable.h"
30
31 #define FC_DIRBAD       1
32 #define FC_DIRUNREAD    2
33 #define FC_DIRUNWRITE   4
34 #define FC_FILEBAD      8
35 #define FC_FILEUNREAD   16
36 #define FC_FILEUNWRITE  32
37
38
39 /*
40  * Forward declarations
41  */
42 #ifdef NEEDPROTOS
43 static unsigned int filecheck( char * );
44 #else /* NEEDPROTOS */
45 static unsigned int filecheck();
46 #endif /* NEEDPROTOS */
47
48
49
50 /*
51  * Take a look around to catch any fatal errors.  For example, make sure the
52  * destination directory for our working files exists, check that all
53  * pathnames make sense, and so on.  Returns 0 is everything's ok,
54  # -1 if there's something wrong which will keep us from functioning
55  * correctly.
56  *
57  * We do all these checks at startup so we can print a reasonable error
58  * message on stderr before we disassociate from the controlling tty.  This
59  * keeps some fatal error messages from "disappearing" into syslog.
60  */
61
62 int
63 sanity()
64 {
65     int err = 0;
66     int rc;
67
68     /*
69      * Are there any replicas listed in the slapd config file?
70      */
71     if ( sglob->replicas == NULL ) {
72         fprintf( stderr, "No replicas in slapd config file \"%s\"!\n",
73             sglob->slapd_configfile );
74         err++;
75     }
76
77     /*
78      * Make sure the directory housing the slapd replogfile exists, and
79      * that the slapd replogfile is readable, if it exists.
80      */
81     if ( sglob->slapd_replogfile == NULL ) {
82         fprintf( stderr, "Fatal error: no \"replogfile\" directive given\n" );
83         err++;
84     } else {
85         rc = filecheck( sglob->slapd_replogfile );
86         if ( rc & FC_DIRBAD ) {
87             fprintf( stderr, "Error: %s: directory does not exist\n", 
88                     sglob->slapd_replogfile );
89             err++;
90         } else if ( rc & FC_DIRUNREAD ) {
91             fprintf( stderr, "Error: %s: directory not readable\n", 
92                     sglob->slapd_replogfile );
93             err++;
94         } else if (!( rc & FC_FILEBAD) && ( rc & FC_FILEUNREAD )) {
95             fprintf( stderr, "Error: %s: file not readable\n", 
96                     sglob->slapd_replogfile );
97             err++;
98         }
99     }
100
101     /*
102      * Make sure the directory for the slurpd replogfile is there, and
103      * that the slurpd replogfile is readable and writable, if it exists.
104      */
105     if ( sglob->slurpd_replogfile == NULL ) {
106         fprintf( stderr, "Fatal error: no \"replogfile\" directive given\n" );
107         err++;
108     } else {
109         rc = filecheck( sglob->slurpd_replogfile );
110         if ( rc & FC_DIRBAD ) {
111             fprintf( stderr, "Error: %s: directory does not exist\n", 
112                     sglob->slurpd_replogfile );
113             err++;
114         } else if ( rc & FC_DIRUNREAD ) {
115             fprintf( stderr, "Error: %s: directory not readable\n", 
116                     sglob->slurpd_replogfile );
117             err++;
118         } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNREAD )) {
119             fprintf( stderr, "Error: %s: file not readable\n", 
120                     sglob->slurpd_replogfile );
121             err++;
122         } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNWRITE )) {
123             fprintf( stderr, "Error: %s: file not writeable\n", 
124                     sglob->slurpd_replogfile );
125             err++;
126         }
127     }
128
129     /*
130      * Make sure that the directory for the slurpd status file is there, and
131      * that the slurpd status file is writable, if it exists.
132      */
133     rc = filecheck( sglob->slurpd_status_file );
134     if ( rc & FC_DIRBAD ) {
135         fprintf( stderr, "Error: %s: directory does not exist\n", 
136                 sglob->slurpd_status_file );
137         err++;
138     } else if ( rc & FC_DIRUNREAD ) {
139         fprintf( stderr, "Error: %s: directory not readable\n", 
140                 sglob->slurpd_status_file );
141         err++;
142     } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNREAD )) {
143         fprintf( stderr, "Error: %s: file not readable\n", 
144                 sglob->slurpd_status_file );
145         err++;
146     } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNWRITE )) {
147         fprintf( stderr, "Error: %s: file not writeable\n", 
148                 sglob->slurpd_status_file );
149         err++;
150     }
151     
152     return ( err == 0 ? 0 : -1 );
153 }
154
155
156
157 /*
158  * Check for the existence of the file and directory leading to the file.
159  * Returns a bitmask which is the logical OR of the following flags:
160  *
161  *  FC_DIRBAD:          directory containing "f" does not exist.
162  *  FC_DIRUNREAD:       directory containing "f" exists but is not readable.
163  *  FC_DIRUNWRITE:      directory containing "f" exists but is not writable.
164  *  FC_FILEBAD:         "f" does not exist.
165  *  FC_FILEUNREAD:      "f" exists but is unreadable.
166  *  FC_FILEUNWRITE:     "f" exists but is unwritable.
167  *
168  * The calling routine is responsible for determining which, if any, of
169  * the returned flags is a problem for a particular file.
170  */
171 static unsigned int
172 filecheck(
173     char        *f
174 )
175 {
176     char                dir[ MAXPATHLEN ];
177     char                *p;
178     unsigned int        ret = 0;
179
180     strcpy( dir, sglob->slapd_replogfile );
181     p = strrchr( dir, '/' );
182     if ( p != NULL ) {
183         *p = '\0';
184     }
185     if ( access( dir, F_OK ) < 0 ) {
186         ret |= FC_DIRBAD;
187     }
188     if ( access( dir, R_OK ) < 0 ) {
189         ret |= FC_DIRUNREAD;
190     }
191     if ( access( dir, W_OK ) < 0 ) {
192         ret |= FC_DIRUNWRITE;
193     }
194     if ( access( f, F_OK ) < 0 ) {
195         ret |= FC_FILEBAD;
196     }
197     if ( access( f, R_OK ) < 0 ) {
198         ret |= FC_FILEUNREAD;
199     }
200     if ( access( f, W_OK ) < 0 ) {
201         ret |= FC_FILEUNWRITE;
202     }
203
204     return ret;
205 }
206