1/*++
2/* NAME
3/*	mail_trigger 3
4/* SUMMARY
5/*	trigger a mail service
6/* SYNOPSIS
7/*	#include <mail_proto.h>
8/*
9/*	int	mail_trigger(class, service, request, length)
10/*	const char *class;
11/*	const char *service;
12/*	const char *request;
13/*	ssize_t	length;
14/* DESCRIPTION
15/*	mail_trigger() wakes up the specified mail subsystem, by
16/*	sending it the specified request. In the case of non-FIFO
17/*	server endpoints, a short-running program should invoke
18/*	event_drain() to ensure proper request delivery.
19/*
20/*	Arguments:
21/* .IP class
22/*	Name of a class of local transport channel endpoints,
23/*	either \fIpublic\fR (accessible by any local user) or
24/*	\fIprivate\fR (administrative access only).
25/* .IP service
26/*	The name of a local transport endpoint within the named class.
27/* .IP request
28/*	A string. The list of valid requests is service specific.
29/* .IP length
30/*	The length of the request string.
31/* DIAGNOSTICS
32/*	The result is -1 in case of problems, 0 otherwise.
33/*	Warnings are logged.
34/* BUGS
35/*	Works with FIFO or UNIX-domain services only.
36/*
37/*	Should use master.cf to find out what transport to use.
38/* SEE ALSO
39/*	fifo_trigger(3) trigger a FIFO-based service
40/*	unix_trigger(3) trigger a UNIX_domain service
41/* LICENSE
42/* .ad
43/* .fi
44/*	The Secure Mailer license must be distributed with this software.
45/* AUTHOR(S)
46/*	Wietse Venema
47/*	IBM T.J. Watson Research
48/*	P.O. Box 704
49/*	Yorktown Heights, NY 10598, USA
50/*--*/
51
52/* System library. */
53
54#include <sys_defs.h>
55#include <sys/stat.h>
56
57/* Utility library. */
58
59#include <msg.h>
60#include <mymalloc.h>
61#include <iostuff.h>
62#include <trigger.h>
63#include <warn_stat.h>
64
65/* Global library. */
66
67#include "mail_params.h"
68#include "mail_proto.h"
69
70/* mail_trigger - trigger a service */
71
72int     mail_trigger(const char *class, const char *service,
73		             const char *req_buf, ssize_t req_len)
74{
75    struct stat st;
76    char   *path;
77    int     status;
78
79    /*
80     * XXX Some systems cannot tell the difference between a named pipe
81     * (fifo) or a UNIX-domain socket. So we may have to try both.
82     */
83    path = mail_pathname(class, service);
84    if ((status = stat(path, &st)) < 0) {
85	 msg_warn("unable to look up %s: %m", path);
86    } else if (S_ISFIFO(st.st_mode)) {
87	status = fifo_trigger(path, req_buf, req_len, var_trigger_timeout);
88	if (status < 0 && S_ISSOCK(st.st_mode))
89	    status = LOCAL_TRIGGER(path, req_buf, req_len, var_trigger_timeout);
90    } else if (S_ISSOCK(st.st_mode)) {
91	status = LOCAL_TRIGGER(path, req_buf, req_len, var_trigger_timeout);
92    } else {
93	msg_warn("%s is not a socket or a fifo", path);
94	status = -1;
95    }
96    myfree(path);
97    return (status);
98}
99