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