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