Copyright updates:
[exim.git] / src / src / enq.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* Copyright (c) The Exim Maintainers 2021 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9 /* Functions concerned with serialization. */
10
11
12 #include "exim.h"
13
14
15
16
17 /*************************************************
18 *       Test for host or ETRN serialization      *
19 *************************************************/
20
21 /* This function is called when a host is listed for serialization of
22 connections. It is also called when ETRN is listed for serialization. We open
23 the misc database and look for a record, which implies an existing connection
24 or ETRN run. If increasing the count would take us past the given limit
25 value return FALSE.  If not, bump it and return TRUE.  If not found, create
26 one with value 1 and return TRUE.
27
28 Arguments:
29   key            string on which to serialize
30   lim            parallelism limit
31
32 Returns:         TRUE if OK to proceed; FALSE otherwise
33 */
34
35
36 BOOL
37 enq_start(uschar *key, unsigned lim)
38 {
39 dbdata_serialize *serial_record;
40 dbdata_serialize new_record;
41 open_db dbblock;
42 open_db *dbm_file;
43
44 DEBUG(D_transport) debug_printf("check serialized: %s\n", key);
45
46 /* Open and lock the waiting information database. The absence of O_CREAT is
47 deliberate; the dbfn_open() function - which is an Exim function - always tries
48 to create if it can't open a read/write file. It expects only O_RDWR or
49 O_RDONLY as its argument. */
50
51 if (!(dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE, TRUE)))
52   return FALSE;
53
54 /* See if there is a record for this host or queue run; if there is, we cannot
55 proceed with the connection unless the record is very old. */
56
57 serial_record = dbfn_read_enforce_length(dbm_file, key, sizeof(dbdata_serialize));
58 if (serial_record && time(NULL) - serial_record->time_stamp < 6*60*60)
59   {
60   if (serial_record->count >= lim)
61     {
62     dbfn_close(dbm_file);
63     DEBUG(D_transport) debug_printf("outstanding serialization record for %s\n",
64       key);
65     return FALSE;
66     }
67   new_record.count = serial_record->count + 1;
68   }
69 else
70   new_record.count = 1;
71
72 /* We can proceed - insert a new record or update the old one. */
73
74 DEBUG(D_transport) debug_printf("write serialization record for %s val %d\n",
75       key, new_record.count);
76 dbfn_write(dbm_file, key, &new_record, (int)sizeof(dbdata_serialize));
77 dbfn_close(dbm_file);
78 return TRUE;
79 }
80
81
82
83 /*************************************************
84 *              Release serialization             *
85 *************************************************/
86
87 /* This function is called when a serialized host's connection or serialized
88 ETRN queue run ends. We open the relevant database and delete its record.
89
90 Arguments:
91   key          the serialization key
92
93 Returns:       nothing
94 */
95
96 void
97 enq_end(uschar *key)
98 {
99 open_db dbblock;
100 open_db *dbm_file;
101 dbdata_serialize *serial_record;
102
103 DEBUG(D_transport) debug_printf("end serialized: %s\n", key);
104
105 if (  !(dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE, TRUE))
106    || !(serial_record = dbfn_read_enforce_length(dbm_file, key, sizeof(dbdata_serialize)))
107    )
108   return;
109 if (--serial_record->count > 0)
110   {
111   DEBUG(D_transport) debug_printf("write serialization record for %s val %d\n",
112       key, serial_record->count);
113   dbfn_write(dbm_file, key, serial_record, (int)sizeof(dbdata_serialize));
114   }
115 else
116   {
117   DEBUG(D_transport) debug_printf("remove serialization record for %s\n", key);
118   dbfn_delete(dbm_file, key);
119   }
120 dbfn_close(dbm_file);
121 }
122
123 /* End of enq.c */