1 /*
2  * (c) Copyright 1992 by Panagiotis Tsirigotis
3  * (c) Sections Copyright 1998-2001 by Rob Braun
4  * All rights reserved.  The file named COPYRIGHT specifies the terms
5  * and conditions for redistribution.
6  */
7 
8 #ifndef SERVICE_H
9 #define SERVICE_H
10 
11 #include "config.h"
12 #include <sys/types.h>
13 #include <netinet/in.h>
14 #include <time.h>
15 
16 #include "defs.h"
17 #include "pset.h"
18 #include "xlog.h"
19 #include "server.h"
20 
21 /*
22  * $Id: service.h,v 1.6 2012-05-09 15:40:29 bbraun Exp $
23  */
24 
25 
26 
27 /*
28  * NOTE: A service can be disabled but not deleted if it has any servers
29  *       running
30  */
31 typedef enum                     /* service states */
32    {
33       SVC_NOT_STARTED = 0,       /* no attempt to start it yet       */
34       SVC_ACTIVE,                /* service is available             */
35       SVC_SUSPENDED,             /* service is suspended             */
36       SVC_DISABLED               /* service disabled                 */
37    } state_e ;
38 
39 
40 /*
41  * NOTE: Clearing the structure will give all its fields their default values
42  */
43 struct service
44 {
45    state_e                svc_state ;
46    int                    svc_ref_count ;   /* # of pters to this struct */
47    struct service_config *svc_conf ;    /* service configuration */
48    int                    svc_fd ;	/* The Listening FD for the service */
49    unsigned               svc_running_servers ;
50    unsigned               svc_retry_servers ;
51    unsigned               svc_attempts ; /* # of attempts to start server */
52    int                    svc_not_generic ; /* 1 spec_service, 0 generic */
53 
54    /*
55     * These fields are used to avoid generating too many messages when
56     * receiving datagrams from a bad address.
57     */
58    union xsockaddr                        *svc_last_dgram_addr ;
59    time_t                                  svc_last_dgram_time ;
60    xlog_h                                  svc_log ;
61 } ;
62 
63 
64 #define SP( p )                  ( (struct service *) (p) )
65 #define SUSPEND( sp )          (sp)->svc_state = SVC_SUSPENDED
66 #define RESUME( sp )           (sp)->svc_state = SVC_ACTIVE
67 
68 
69 /*
70  * Field access macros
71  */
72 #define SVC_CONF( sp )             ( (sp)->svc_conf )
73 #define SVC_FD( sp )               ( (sp)->svc_fd )
74 #define SVC_RUNNING_SERVERS( sp )  (sp)->svc_running_servers
75 #define SVC_RETRIES( sp )          (sp)->svc_retry_servers
76 #define SVC_LOG( sp )              (sp)->svc_log
77 #define SVC_REFCOUNT( sp )         (sp)->svc_ref_count
78 #define SVC_ID( sp )               SC_ID( SVC_CONF( sp ) )
79 #define SVC_SOCKET_TYPE( sp )      SC_SOCKET_TYPE( SVC_CONF( sp ) )
80 #define SVC_STATE( sp )            (sp)->svc_state
81 #define SVC_ATTEMPTS( sp )         (sp)->svc_attempts
82 #define SVC_LAST_DGRAM_ADDR( sp )  (sp)->svc_last_dgram_addr
83 #define SVC_LAST_DGRAM_TIME( sp )  (sp)->svc_last_dgram_time
84 #define SVC_NOT_GENERIC( sp )      (sp)->svc_not_generic
85 
86 #define SVC_IS_ACTIVE( sp )      ( (sp)->svc_state == SVC_ACTIVE )
87 #define SVC_IS_SUSPENDED( sp )   ( (sp)->svc_state == SVC_SUSPENDED )
88 #define SVC_IS_AVAILABLE( sp )   ( SVC_IS_ACTIVE(sp) || SVC_IS_SUSPENDED(sp) )
89 #define SVC_IS_DISABLED( sp )    ( (sp)->svc_state == SVC_DISABLED )
90 #define SVC_IS_MUXCLIENT( sp )   ( SC_IS_MUXCLIENT( SVC_CONF ( sp ) ) )
91 #define SVC_IS_MUXPLUSCLIENT(sp) ( SC_IS_MUXPLUSCLIENT( SVC_CONF ( sp ) ) )
92 #define SVC_IS_TCPMUX( sp )      ( SC_IS_TCPMUX( SVC_CONF ( sp ) ) )
93 
94 #define TCPMUX_ACK "+Go\r\n"
95 #define TCPMUX_NOT_FOUND "-Service name not found\r\n"
96 /*
97  * Predicate checking macros
98  */
99 #define SVC_FORKS( sp )            SC_FORKS( SVC_CONF( sp ) )
100 #define SVC_RETRY( sp )            SC_RETRY( SVC_CONF( sp ) )
101 #define SVC_WAITS( sp )            SC_WAITS( SVC_CONF( sp ) )
102 #define SVC_IS_INTERCEPTED( sp )   SC_IS_INTERCEPTED( SVC_CONF( sp ) )
103 #define SVC_ACCEPTS_CONNECTIONS( sp )   \
104                                    SC_ACCEPTS_CONNECTIONS( SVC_CONF( sp ) )
105 
106 #define SVC_IS_LOGGING( sp )       ( (sp)->svc_log != NULL )
107 #define SVC_LOGS_ON_SUCCESS( sp )         \
108       ( SVC_IS_LOGGING( sp ) && SC_LOGS_ON_SUCCESS( SVC_CONF( sp ) ) )
109 #define SVC_LOGS_ON_FAILURE( sp )         \
110       ( SVC_IS_LOGGING( sp ) && SC_LOGS_ON_FAILURE( SVC_CONF( sp ) ) )
111 #define SVC_LOGS_ON_EXIT( sp )            \
112       ( SVC_IS_LOGGING( sp ) && SC_LOGS_ON_EXIT( SVC_CONF( sp ) ) )
113 #define SVC_LOGS_USERID_ON_SUCCESS( sp )   \
114       ( SVC_IS_LOGGING( sp ) && SC_LOGS_USERID_ON_SUCCESS( SVC_CONF( sp ) ) )
115 #define SVC_LOGS_USERID_ON_FAILURE( sp )   \
116       ( SVC_IS_LOGGING( sp ) && SC_LOGS_USERID_ON_FAILURE( SVC_CONF( sp ) ) )
117 
118 /*
119  * Reference counting macros
120  */
121 #define SVC_HOLD( sp )            (sp)->svc_ref_count++
122 #define SVC_RELE( sp )            ( --(sp)->svc_ref_count )
123 
124 
125 #define SVC_INTERNAL( sp, serp )     SC_INTERNAL( SVC_CONF( sp ), serp )
126 #define SVC_MAKE_EXTERNAL( sp )      SC_MAKE_EXTERNAL( SVC_CONF( sp ) )
127 
128 #define SVC_DEC_RUNNING_SERVERS( sp )                                         \
129    {                                                                          \
130       if ( SVC_RUNNING_SERVERS( sp ) != 0 )                                   \
131          (sp)->svc_running_servers-- ;                                        \
132       else                                                                    \
133          msg( LOG_ERR, func,                                                  \
134             "Service %s: server exit with 0 running servers", SVC_ID( sp ) ) ;\
135    }
136 
137 #define SVC_INC_RUNNING_SERVERS( sp )       (sp)->svc_running_servers++
138 
139 #define SVC_INC_RETRIES( sp )               (sp)->svc_retry_servers++
140 #define SVC_DEC_RETRIES( sp )               (sp)->svc_retry_servers--
141 
142 struct service *svc_new(struct service_config *scp);
143 struct service *svc_make_special(struct service_config *scp);
144 void svc_free(struct service *sp);
145 status_e svc_activate(struct service *sp);
146 void svc_deactivate(struct service *sp);
147 void svc_suspend(struct service *sp);
148 void svc_resume(struct service *sp);
149 int svc_release(struct service *sp);
150 void svc_dump(const struct service *sp,int fd);
151 void svc_request(struct service *sp);
152 status_e svc_generic_handler( struct service *sp, connection_s *cp );
153 status_e svc_parent_access_control(struct service *sp,connection_s *cp);
154 status_e svc_child_access_control(struct service *sp,connection_s *cp);
155 void svc_postmortem(struct service *sp,struct server *serp);
156 void close_all_svc_descriptors(void);
157 
158 #endif   /* SERVICE_H */
159