]> git.sur5r.net Git - openldap/blob - servers/slurpd/reject.c
Rework test suite to use run script.
[openldap] / servers / slurpd / reject.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1996 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18
19 /*
20  * reject.c - routines to write replication records to reject files.
21  * An Re struct is writted to a reject file if it cannot be propagated
22  * to a replica LDAP server.
23  */
24
25 #include "portable.h"
26
27 #include <stdio.h>
28
29 #include <ac/stdlib.h>
30 #include <ac/errno.h>
31 #include <ac/unistd.h>
32
33 #include <sys/stat.h>
34 #include <fcntl.h>
35
36 #include "slurp.h"
37 #include "globals.h"
38
39 #ifdef _WIN32
40 #define PORTSEP ","
41 #else
42 #define PORTSEP ":"
43 #endif
44
45 /*
46  * Write a replication record to a reject file.  The reject file has the
47  * same name as the replica's private copy of the file but with ".rej"
48  * appended (e.g. "/usr/tmp/<hostname>:<port>.rej")
49  *
50  * If errmsg is non-NULL, use that as the error message in the reject
51  * file.  Otherwise, use ldap_err2string( lderr ).
52  */
53 void
54 write_reject(
55     Ri          *ri,
56     Re          *re,
57     int         lderr,
58     char        *errmsg
59 )
60 {
61     char        rejfile[ MAXPATHLEN ];
62     FILE        *rfp, *lfp;
63     int         rc;
64
65     ldap_pvt_thread_mutex_lock( &sglob->rej_mutex );
66     snprintf( rejfile, sizeof rejfile, "%s" LDAP_DIRSEP "%s" PORTSEP "%d.rej",
67                 sglob->slurpd_rdir, ri->ri_hostname, ri->ri_port );
68
69     if ( access( rejfile, F_OK ) < 0 ) {
70         /* Doesn't exist - try to create */
71         int rjfd;
72         if (( rjfd = open( rejfile, O_RDWR|O_APPEND|O_CREAT|O_EXCL,
73                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP )) < 0 ) {
74 #ifdef NEW_LOGGING
75                 LDAP_LOG ( SLURPD, ERR, "write_reject: "
76                         "Error: Cannot create \"%s\":%s\n", 
77                         rejfile, sys_errlist[ errno ], 0 );
78 #else
79             Debug( LDAP_DEBUG_ANY,
80                 "Error: write_reject: Cannot create \"%s\": %s\n",
81                 rejfile, sys_errlist[ errno ], 0 );
82 #endif
83             ldap_pvt_thread_mutex_unlock( &sglob->rej_mutex );
84             return;
85         } else {
86             close( rjfd );
87         }
88     }
89     if (( rc = acquire_lock( rejfile, &rfp, &lfp )) < 0 ) {
90 #ifdef NEW_LOGGING
91         LDAP_LOG ( SLURPD, ERR, "write_reject: "
92                 "Error: Cannot open reject file \"%s\"\n", rejfile, 0, 0 );
93 #else
94         Debug( LDAP_DEBUG_ANY, "Error: cannot open reject file \"%s\"\n",
95                 rejfile, 0, 0 );
96 #endif
97     } else {
98         fseek( rfp, 0, 2 );
99         if ( errmsg != NULL ) {
100             fprintf( rfp, "%s: %s\n", ERROR_STR, errmsg );
101         } else {
102             fprintf( rfp, "%s: %s\n", ERROR_STR, ldap_err2string( lderr ));
103         }
104         if ((rc = re->re_write( ri, re, rfp )) < 0 ) {
105 #ifdef NEW_LOGGING
106                 LDAP_LOG ( SLURPD, ERR, "write_reject: "
107                         "Error: Cannot write reject file \"%s\"\n", rejfile, 0, 0 );
108 #else
109             Debug( LDAP_DEBUG_ANY,
110                     "Error: cannot write reject file \"%s\"\n",
111                     rejfile, 0, 0 );
112 #endif
113         }
114         (void) relinquish_lock( rejfile, rfp, lfp );
115 #ifdef NEW_LOGGING
116         LDAP_LOG ( SLURPD, ERR, "write_reject: "
117                 "Error: ldap operation failed, data written to \"%s\"\n", 
118                 rejfile, 0, 0 );
119 #else
120         Debug( LDAP_DEBUG_ANY,
121                 "Error: ldap operation failed, data written to \"%s\"\n",
122                 rejfile, 0, 0 );
123 #endif
124     }
125     ldap_pvt_thread_mutex_unlock( &sglob->rej_mutex );
126     return;
127 }
128