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