Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / slaptest.c
blobc47abf85d78098755cd962b63162a42678fa6a04
1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 * Copyright 2004-2008 The OpenLDAP Foundation.
4 * Portions Copyright 2004 Pierangelo Masarati.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
9 * Public License.
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* ACKNOWLEDGEMENTS:
16 * This work was initially developed by Pierangelo Masarati for inclusion
17 * in OpenLDAP Software.
20 #include "portable.h"
22 #include <stdio.h>
24 #include <ac/stdlib.h>
26 #include <ac/ctype.h>
27 #include <ac/string.h>
28 #include <ac/socket.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <ac/unistd.h>
32 #include <ac/errno.h>
34 #include <lber.h>
35 #include <ldif.h>
36 #include <lutil.h>
38 #include "slapcommon.h"
40 static int
41 test_file( const char *fname, const char *ftype )
43 struct stat st;
44 int save_errno;
46 switch ( stat( fname, &st ) ) {
47 case 0:
48 if ( !( st.st_mode & S_IWRITE ) ) {
49 Debug( LDAP_DEBUG_ANY, "%s file "
50 "\"%s\" exists, but user does not have access\n",
51 ftype, fname, 0 );
52 return -1;
54 break;
56 case -1:
57 default:
58 save_errno = errno;
59 if ( save_errno == ENOENT ) {
60 FILE *fp = fopen( fname, "w" );
62 if ( fp == NULL ) {
63 save_errno = errno;
65 Debug( LDAP_DEBUG_ANY, "unable to open file "
66 "\"%s\": %d (%s)\n",
67 fname,
68 save_errno, strerror( save_errno ) );
70 return -1;
72 fclose( fp );
73 unlink( fname );
74 break;
77 Debug( LDAP_DEBUG_ANY, "unable to stat file "
78 "\"%s\": %d (%s)\n",
79 slapd_pid_file,
80 save_errno, strerror( save_errno ) );
81 return -1;
84 return 0;
87 int
88 slaptest( int argc, char **argv )
90 int rc = EXIT_SUCCESS;
91 const char *progname = "slaptest";
93 slap_tool_init( progname, SLAPTEST, argc, argv );
95 if ( slapd_pid_file != NULL ) {
96 if ( test_file( slapd_pid_file, "pid" ) ) {
97 return EXIT_FAILURE;
101 if ( slapd_args_file != NULL ) {
102 if ( test_file( slapd_args_file, "args" ) ) {
103 return EXIT_FAILURE;
107 if ( !quiet ) {
108 fprintf( stderr, "config file testing succeeded\n");
111 slap_tool_destroy();
113 return rc;