]> git.sur5r.net Git - openldap/blob - servers/slurpd/sanity.c
Replaced PREEMPTIVE_THREADS with HAVE_YIELDING_SELECT to clarify
[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
25 #include <ac/unistd.h>
26 #include <ac/string.h>
27
28 #include "slurp.h"
29 #include "globals.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 static unsigned int filecheck LDAP_P(( char * ));
43
44
45
46 /*
47  * Take a look around to catch any fatal errors.  For example, make sure the
48  * destination directory for our working files exists, check that all
49  * pathnames make sense, and so on.  Returns 0 is everything's ok,
50  # -1 if there's something wrong which will keep us from functioning
51  * correctly.
52  *
53  * We do all these checks at startup so we can print a reasonable error
54  * message on stderr before we disassociate from the controlling tty.  This
55  * keeps some fatal error messages from "disappearing" into syslog.
56  */
57
58 int
59 sanity( void )
60 {
61     int err = 0;
62     int rc;
63
64     /*
65      * Are there any replicas listed in the slapd config file?
66      */
67     if ( sglob->replicas == NULL ) {
68         fprintf( stderr, "No replicas in slapd config file \"%s\"!\n",
69             sglob->slapd_configfile );
70         err++;
71     }
72
73     /*
74      * Make sure the directory housing the slapd replogfile exists, and
75      * that the slapd replogfile is readable, if it exists.
76      */
77     if ( sglob->slapd_replogfile == NULL ) {
78         fprintf( stderr, "Fatal error: no \"replogfile\" directive given\n" );
79         err++;
80     } else {
81         rc = filecheck( sglob->slapd_replogfile );
82         if ( rc & FC_DIRBAD ) {
83             fprintf( stderr, "Error: %s: directory does not exist\n", 
84                     sglob->slapd_replogfile );
85             err++;
86         } else if ( rc & FC_DIRUNREAD ) {
87             fprintf( stderr, "Error: %s: directory not readable\n", 
88                     sglob->slapd_replogfile );
89             err++;
90         } else if (!( rc & FC_FILEBAD) && ( rc & FC_FILEUNREAD )) {
91             fprintf( stderr, "Error: %s: file not readable\n", 
92                     sglob->slapd_replogfile );
93             err++;
94         }
95     }
96
97     /*
98      * Make sure the directory for the slurpd replogfile is there, and
99      * that the slurpd replogfile is readable and writable, if it exists.
100      */
101     if ( sglob->slurpd_replogfile == NULL ) {
102         fprintf( stderr, "Fatal error: no \"replogfile\" directive given\n" );
103         err++;
104     } else {
105         rc = filecheck( sglob->slurpd_replogfile );
106         if ( rc & FC_DIRBAD ) {
107             fprintf( stderr, "Error: %s: directory does not exist\n", 
108                     sglob->slurpd_replogfile );
109             err++;
110         } else if ( rc & FC_DIRUNREAD ) {
111             fprintf( stderr, "Error: %s: directory not readable\n", 
112                     sglob->slurpd_replogfile );
113             err++;
114         } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNREAD )) {
115             fprintf( stderr, "Error: %s: file not readable\n", 
116                     sglob->slurpd_replogfile );
117             err++;
118         } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNWRITE )) {
119             fprintf( stderr, "Error: %s: file not writeable\n", 
120                     sglob->slurpd_replogfile );
121             err++;
122         }
123     }
124
125     /*
126      * Make sure that the directory for the slurpd status file is there, and
127      * that the slurpd status file is writable, if it exists.
128      */
129     rc = filecheck( sglob->slurpd_status_file );
130     if ( rc & FC_DIRBAD ) {
131         fprintf( stderr, "Error: %s: directory does not exist\n", 
132                 sglob->slurpd_status_file );
133         err++;
134     } else if ( rc & FC_DIRUNREAD ) {
135         fprintf( stderr, "Error: %s: directory not readable\n", 
136                 sglob->slurpd_status_file );
137         err++;
138     } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNREAD )) {
139         fprintf( stderr, "Error: %s: file not readable\n", 
140                 sglob->slurpd_status_file );
141         err++;
142     } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNWRITE )) {
143         fprintf( stderr, "Error: %s: file not writeable\n", 
144                 sglob->slurpd_status_file );
145         err++;
146     }
147     
148     return ( err == 0 ? 0 : -1 );
149 }
150
151
152
153 /*
154  * Check for the existence of the file and directory leading to the file.
155  * Returns a bitmask which is the logical OR of the following flags:
156  *
157  *  FC_DIRBAD:          directory containing "f" does not exist.
158  *  FC_DIRUNREAD:       directory containing "f" exists but is not readable.
159  *  FC_DIRUNWRITE:      directory containing "f" exists but is not writable.
160  *  FC_FILEBAD:         "f" does not exist.
161  *  FC_FILEUNREAD:      "f" exists but is unreadable.
162  *  FC_FILEUNWRITE:     "f" exists but is unwritable.
163  *
164  * The calling routine is responsible for determining which, if any, of
165  * the returned flags is a problem for a particular file.
166  */
167 static unsigned int
168 filecheck(
169     char        *f
170 )
171 {
172     char                dir[ MAXPATHLEN ];
173     char                *p;
174     unsigned int        ret = 0;
175
176     strcpy( dir, sglob->slapd_replogfile );
177     p = strrchr( dir, '/' );
178     if ( p != NULL ) {
179         *p = '\0';
180     }
181     if ( access( dir, F_OK ) < 0 ) {
182         ret |= FC_DIRBAD;
183     }
184     if ( access( dir, R_OK ) < 0 ) {
185         ret |= FC_DIRUNREAD;
186     }
187     if ( access( dir, W_OK ) < 0 ) {
188         ret |= FC_DIRUNWRITE;
189     }
190     if ( access( f, F_OK ) < 0 ) {
191         ret |= FC_FILEBAD;
192     }
193     if ( access( f, R_OK ) < 0 ) {
194         ret |= FC_FILEUNREAD;
195     }
196     if ( access( f, W_OK ) < 0 ) {
197         ret |= FC_FILEUNWRITE;
198     }
199
200     return ret;
201 }