]> git.sur5r.net Git - openldap/blob - servers/slurpd/reject.c
Merge termination bugfix from main. Provided by <vasquez@w270.de>.
[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
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26
27 #include "slurp.h"
28 #include "globals.h"
29
30 #ifndef SYSERRLIST_IN_STDIO
31 extern char *sys_errlist[];
32 #endif /* SYSERRLIST_IN_STDIO */
33
34
35 /*
36  * Write a replication record to a reject file.  The reject file has the
37  * same name as the replica's private copy of the file but with ".rej"
38  * appended (e.g. "/usr/tmp/<hostname>:<port>.rej")
39  *
40  * If errmsg is non-NULL, use that as the error message in the reject
41  * file.  Otherwise, use ldap_err2string( lderr ).
42  */
43 void
44 write_reject(
45     Ri          *ri,
46     Re          *re,
47     int         lderr,
48     char        *errmsg
49 )
50 {
51     char        rejfile[ MAXPATHLEN ];
52     FILE        *rfp, *lfp;
53     int         rc;
54
55     pthread_mutex_lock( &sglob->rej_mutex );
56     sprintf( rejfile, "%s/%s:%d.rej", sglob->slurpd_rdir,
57             ri->ri_hostname, ri->ri_port );
58
59     if ( access( rejfile, F_OK ) < 0 ) {
60         /* Doesn't exist - try to create */
61         int rjfd;
62         if (( rjfd = open( rejfile, O_RDWR | O_APPEND | O_CREAT,
63                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP )) < 0 ) {
64             Debug( LDAP_DEBUG_ANY,
65                 "Error: write_reject: Cannot create \"%s\": %s\n",
66                 rejfile, sys_errlist[ errno ], 0 );
67             pthread_mutex_unlock( &sglob->rej_mutex );
68             return;
69         } else {
70             close( rjfd );
71         }
72     }
73     if (( rc = acquire_lock( rejfile, &rfp, &lfp )) < 0 ) {
74         Debug( LDAP_DEBUG_ANY, "Error: cannot open reject file \"%s\"\n",
75                 rejfile, 0, 0 );
76     } else {
77         fseek( rfp, 0, 2 );
78         if ( errmsg != NULL ) {
79             fprintf( rfp, "%s: %s\n", ERROR_STR, errmsg );
80         } else {
81             fprintf( rfp, "%s: %s\n", ERROR_STR, ldap_err2string( lderr ));
82         }
83         if ((rc = re->re_write( ri, re, rfp )) < 0 ) {
84             Debug( LDAP_DEBUG_ANY,
85                     "Error: cannot write reject file \"%s\"\n",
86                     rejfile, 0, 0 );
87         }
88         (void) relinquish_lock( rejfile, rfp, lfp );
89         Debug( LDAP_DEBUG_ANY,
90                 "Error: ldap operation failed, data written to \"%s\"\n",
91                 rejfile, 0, 0 );
92     }
93     pthread_mutex_unlock( &sglob->rej_mutex );
94     return;
95 }
96