]> git.sur5r.net Git - openldap/blob - servers/slurpd/reject.c
Cleaned up getdn normalization
[openldap] / servers / slurpd / reject.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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/errno.h>
30 #include <ac/unistd.h>
31
32 #include <sys/stat.h>
33 #include <fcntl.h>
34
35 #include "slurp.h"
36 #include "globals.h"
37
38 /*
39  * Write a replication record to a reject file.  The reject file has the
40  * same name as the replica's private copy of the file but with ".rej"
41  * appended (e.g. "/usr/tmp/<hostname>:<port>.rej")
42  *
43  * If errmsg is non-NULL, use that as the error message in the reject
44  * file.  Otherwise, use ldap_err2string( lderr ).
45  */
46 void
47 write_reject(
48     Ri          *ri,
49     Re          *re,
50     int         lderr,
51     char        *errmsg
52 )
53 {
54     char        rejfile[ MAXPATHLEN ];
55     FILE        *rfp, *lfp;
56     int         rc;
57
58     ldap_pvt_thread_mutex_lock( &sglob->rej_mutex );
59     sprintf( rejfile, "%s/%s:%d.rej", sglob->slurpd_rdir,
60             ri->ri_hostname, ri->ri_port );
61
62     if ( access( rejfile, F_OK ) < 0 ) {
63         /* Doesn't exist - try to create */
64         int rjfd;
65         if (( rjfd = open( rejfile, O_RDWR | O_APPEND | O_CREAT,
66                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP )) < 0 ) {
67 #ifdef NEW_LOGGING
68                 LDAP_LOG (( "reject", LDAP_LEVEL_ERR, "write_reject: "
69                         "Error: Cannot create \"%s\":%s\n", 
70                         rejfile, sys_errlist[ errno ] ));
71 #else
72             Debug( LDAP_DEBUG_ANY,
73                 "Error: write_reject: Cannot create \"%s\": %s\n",
74                 rejfile, sys_errlist[ errno ], 0 );
75 #endif
76             ldap_pvt_thread_mutex_unlock( &sglob->rej_mutex );
77             return;
78         } else {
79             close( rjfd );
80         }
81     }
82     if (( rc = acquire_lock( rejfile, &rfp, &lfp )) < 0 ) {
83 #ifdef NEW_LOGGING
84         LDAP_LOG (( "reject", LDAP_LEVEL_ERR, "write_reject: "
85                 "Error: Cannot open reject file \"%s\"\n", rejfile ));
86 #else
87         Debug( LDAP_DEBUG_ANY, "Error: cannot open reject file \"%s\"\n",
88                 rejfile, 0, 0 );
89 #endif
90     } else {
91         fseek( rfp, 0, 2 );
92         if ( errmsg != NULL ) {
93             fprintf( rfp, "%s: %s\n", ERROR_STR, errmsg );
94         } else {
95             fprintf( rfp, "%s: %s\n", ERROR_STR, ldap_err2string( lderr ));
96         }
97         if ((rc = re->re_write( ri, re, rfp )) < 0 ) {
98 #ifdef NEW_LOGGING
99                 LDAP_LOG (( "reject", LDAP_LEVEL_ERR, "write_reject: "
100                         "Error: Cannot write reject file \"%s\"\n", rejfile ));
101 #else
102             Debug( LDAP_DEBUG_ANY,
103                     "Error: cannot write reject file \"%s\"\n",
104                     rejfile, 0, 0 );
105 #endif
106         }
107         (void) relinquish_lock( rejfile, rfp, lfp );
108 #ifdef NEW_LOGGING
109         LDAP_LOG (( "reject", LDAP_LEVEL_ERR, "write_reject: "
110                 "Error: ldap operation failed, data written to \"%s\"\n", rejfile ));
111 #else
112         Debug( LDAP_DEBUG_ANY,
113                 "Error: ldap operation failed, data written to \"%s\"\n",
114                 rejfile, 0, 0 );
115 #endif
116     }
117     ldap_pvt_thread_mutex_unlock( &sglob->rej_mutex );
118     return;
119 }
120