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