## `courierd` daemon process

### Configuration files

The following files are present in the `local/config` subdirectory.

The following configuration files are specific to the main `courierd` daemon. Module-specific configuration files are defined elsewhere.

`me` - if present, specifies the hostname of the machine, and used instead of `gethostname()`. Note that this is also used by submit. If The Courier mail server's installation directory is exported to clusters, you must arrange for a different `local/config/me` file for each machine in the cluster. One way to do so is to have `local/config/me` set as a soft link to /etc/HOSTNAME, which on each machine contains its host name.

`locals` - all the possible domains that are recognized as local. Text file, one domain per line. Defaults to me. This is used by the locals module, and by submit.

`queuetime` - how long The Courier mail server keeps trying to deliver a message, before returning it as undeliverable. Default is one week.

`warntime` - if a message is undeliverable for this long, send a warning message to the sender. Set to 0, in order to disable. Default is four hours.

`sizelimit` - maximum size of a message, in bytes. `submit` rejects longer messages.

`sizecheck` - contains three numbers: b i s. submit will return a temporary error indication and reject a submission if there are less than b free blocks, or i free inodes, on the mail filesystem. As submit receives the message, it will check the status of the filesystem every s bytes, and will immediately stop if the available free space falls below the set limits. If not specified, the limits are 500 blocks, 20 inodes, and 128K bytes. Also note that sometimes the actual amount of free disk blocks has to be twice as much, if `submit` decides to rewrite the message to convert to/from quoted printable encoding, or to add MIME headers.

`batchsize` - maximum number of recipients per message. submit splits longer recipient lists into separate messages. If absent, defaults to 100.

`queuelo` - `courierd` output queue low watermark. If absent, defaults to the sum of MAXDELS of all output modules. This number should be the minimum value for queuelo. The minimum value for `queuelo` is 20.

`queuehi` - `courierd` output queue high watermark. If absent, defaults to queuelo * 2, not to exceed queuelo + 1000. Must greater than queuelo.

`respawnlo` - do not respawn `courierd` second process more often than this time interval. The contents of this file is a single number following by 'h' for hours or 'd' for days. If this file does not exist, the `respawnlo` is set for one hour.

`respawnhi` - respawn `courierd` at least this often. Helps to cut down on memory fragmentation. The contents of this file is a single number following by 'h' for hours or 'd' for days. If this file does not exist, the `respawnhi` is set for seven days.

`bouncefrom` - what to put in the From: header on bounces

## `courierd` - process A - startup process

### `courierd` startup activity

A) chdirs to ${COURIER_HOME}. Disassociates from controlling terminal. Closes stdin/stdout/stderr, reopens stdin to `/dev/null`, stdout to `/dev/console`., and stderror to `logger`, Resets umask to 007. Forks, parent exits, child continues to run, becomes the first `courierd` daemon process, process A.

B) main starts all output modules. A separate pipe to each output module's stdin. Shared pipe from all output modules' stdout.

C) main setgids and setuids itself to mail.

D) main creates another pipe, then forks the second `courierd` daemon process, process B.

E) main waits for any child process to terminate. If main receives a SIGHUP, main sighups process B.

F) if process B terminates with a zero exit, main respawns it again, and logs the event to syslog.

G) All other signals are logged to syslog, resulting in termination.

H) Non-zero exit codes are logged, process A waits a minute, then respawns process B.

I) each time main forks process B, main executes tmp directory cleanup (see submit process).

### `courierd` - process B

A) Receipt of a SIGHUP sets a boolean flag called 'termination_flag'. This is logged.

B) Allocate queuehi msgq structures.

C) Open all shared module libraries.

D) Open `queue/trigger` for reading.

E) Remove all empty directories in `local/msgq` and `local/msgs`.

F) Queue scan.

> Queue scan A) Obtain current time B) Read all directories in `local/msgq`, sort it in chronological order. C) Read each directory, skip control files which are already in the queue (check using inodes), sort the remaining ones in chronological order. Perform the following steps for each remaining control file. D) Add this control file to the queue. Stop queue scan if the control file wasn't added.

> Add control file to the queue A) Read the control file. B) Initialize the msgq structure, and all the rcptinfos. If there are no rcptinfos, remove control file from the queue. C) If the msgq array is not full, go to step E. D) Scan the msgq array, by nextdel, starting with the latest nextdel. Stop at the first msgq whose rcptinfos all have non-null pending pointers. If we don't find one, stop. Use pendingpos to remove the rcptinfo * from pending->recipient_list. If the resulting recipient_list is NULL: if hostp is not null, set hostp->pendinglist to NULL; remove this pendelinfo structure from drvp->pendelinfo_list. Remove this msgq. E) Add the msgq structure to the queue list, sorted by nextdel. For each rcptinfo, search delmodule's hdlvrpfirst/last list for this host. If found, go to step F. If not found, check pendelinfo_list. If found there, add this rcptinfo * to pendelinfo's recipient_list (sorted by nextdel). Otherwise, search hdlvrpfirst/last list from the tail end, looking for a host with a 0 dlvrcount and either a null pendinglist pointer, or whose pendelinfo's first recipient has a later timestamp than the new control file. If all hosts have a non-0 dlvrcount, create a new pendelinfo, add it to pendelinfo_list, add this rcptinfo * to pendelinfo's recipient_list (sorted by nextdel). If found one with a 0 dlvrcount, set pendelinfo->hostp to null (if pendelinfo is not null). F) If delinfo's delpfreefirst is null, add this rcptinfo * to dlrvrhost's pendelinfo->recipient_list, otherwise remove the first delinfo from delpfreefirst's list, initialize it, increment dlvrhost's dlvrcount, send the message to the device module.

> Remove control file from the queue A) Read the control file and check if every recipient has a succesfull delivery. B) Unless every recipient has a succesful delivery, generate a bounce message. C) Remove the message file, and both links to the control file. Deallocate the msgq structure. If the msgq list falls below respawnlo, set the queue scan flag.

### `courierd` - process B - main loop

A) `courierd` scans tmp directory for new messages (see submit process, above). When a new message is found, the message and control files are moved to local/msgs, and the control file is linked to local/msgq, with the current time set as the next scheduled delivery time. If the termination flag is NOT set, add this control file to the queue.

B) If the termination flag is not set, and there are no pending deliveries, check the process starting time. If the process has been running for at least respawnlo period of time, set the termination flag.

C) If the process has been running for at least respawnhi period of time, set the termination flag unconditionally.

D) If the termination flag is not set, and the queue scan flag is set, do a queue scan. If there are any control files in the local/tmp directory, go back to step A.

E) If the termination flag is set, wait until a delivery completion notice is received. If there are no pending deliveries, terminate the process normally.

F) Wait until any of the following events: delivery completion notice, new message notification from queue/trigger, if there are delivery attempts scheduled for a future time, wait until the timer expires. Check the time this process was started. If respawnlo interval has not passed, wait until respawnlo interval passes.

G) End of file indication from process A causes an immediate termination.

### Delivery completion notice

A) Get delid of the completed delivery.

B) Decrement delinfo->rcptlist->dlvrcount. If it's zero, recycle the msgq, reread the control file. If all recipients have a delivery completion or failure on file, or if the message's expiration time has expired, remove control file from the queue.

C) Put the delinfo back on the delpfreefirst list. Move the delinfo's dlvrhost to the top of the MRU list.

D) As long as the delpfreefirst list is not empty, do this. Scan the MRU list, from top to bottom. If we find a dlvrhost with a pending list, remove the first rcptinfo from the rcptinfo_list (if it's empty, remove the pendelinfo structure), remove the first delinfo from delpfreefirst's list, initialize it, increment dlvrhost's dlvrcount, send the message to the device module. If all dlvrhosts have null pending lists, go through the drvinfo's pendelinfo_list.