]> git.sur5r.net Git - openldap/blob - servers/slapd/slaptest.c
Check rc in prev commit
[openldap] / servers / slapd / slaptest.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2004-2009 The OpenLDAP Foundation.
5  * Portions Copyright 2004 Pierangelo Masarati.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Pierangelo Masarati for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24
25 #include <ac/stdlib.h>
26
27 #include <ac/ctype.h>
28 #include <ac/string.h>
29 #include <ac/socket.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <ac/unistd.h>
33 #include <ac/errno.h>
34
35 #include <lber.h>
36 #include <ldif.h>
37 #include <lutil.h>
38
39 #include "slapcommon.h"
40
41 static int
42 test_file( const char *fname, const char *ftype )
43 {
44         struct stat     st;
45         int             save_errno;
46
47         switch ( stat( fname, &st ) ) {
48         case 0:
49                 if ( !( st.st_mode & S_IWRITE ) ) {
50                         Debug( LDAP_DEBUG_ANY, "%s file "
51                                 "\"%s\" exists, but user does not have access\n",
52                                 ftype, fname, 0 );
53                         return -1;
54                 }
55                 break;
56
57         case -1:
58         default:
59                 save_errno = errno;
60                 if ( save_errno == ENOENT ) {
61                         FILE            *fp = fopen( fname, "w" );
62
63                         if ( fp == NULL ) {
64                                 save_errno = errno;
65
66                                 Debug( LDAP_DEBUG_ANY, "unable to open file "
67                                         "\"%s\": %d (%s)\n",
68                                         fname,
69                                         save_errno, strerror( save_errno ) );
70
71                                 return -1;
72                         }
73                         fclose( fp );
74                         unlink( fname );
75                         break;
76                 }
77
78                 Debug( LDAP_DEBUG_ANY, "unable to stat file "
79                         "\"%s\": %d (%s)\n",
80                         slapd_pid_file,
81                         save_errno, strerror( save_errno ) );
82                 return -1;
83         }
84
85         return 0;
86 }
87
88 int
89 slaptest( int argc, char **argv )
90 {
91         int                     rc = EXIT_SUCCESS;
92         const char              *progname = "slaptest";
93
94         slap_tool_init( progname, SLAPTEST, argc, argv );
95
96         if ( slapd_pid_file != NULL ) {
97                 if ( test_file( slapd_pid_file, "pid" ) ) {
98                         return EXIT_FAILURE;
99                 }
100         }
101
102         if ( slapd_args_file != NULL ) {
103                 if ( test_file( slapd_args_file, "args" ) ) {
104                         return EXIT_FAILURE;
105                 }
106         }
107
108         if ( !quiet ) {
109                 fprintf( stderr, "config file testing succeeded\n");
110         }
111
112         if ( slap_tool_destroy())
113                 rc = EXIT_FAILURE;
114
115         return rc;
116 }