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