1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 
9 
10 static const nxt_service_t  nxt_services[] = {
11 
12 #if (NXT_HAVE_KQUEUE)
13     { "engine", "kqueue", &nxt_kqueue_engine },
14 #endif
15 
16 #if (NXT_HAVE_EPOLL_EDGE)
17     { "engine", "epoll", &nxt_epoll_edge_engine },
18     { "engine", "epoll_edge", &nxt_epoll_edge_engine },
19     { "engine", "epoll_level", &nxt_epoll_level_engine },
20 
21 #elif (NXT_HAVE_EPOLL)
22     { "engine", "epoll", &nxt_epoll_level_engine },
23     { "engine", "epoll_level", &nxt_epoll_level_engine },
24 #endif
25 
26 #if (NXT_HAVE_EVENTPORT)
27     { "engine", "eventport", &nxt_eventport_engine },
28 #endif
29 
30 #if (NXT_HAVE_DEVPOLL)
31     { "engine", "devpoll", &nxt_devpoll_engine },
32     { "engine", "/dev/poll", &nxt_devpoll_engine },
33 #endif
34 
35 #if (NXT_HAVE_POLLSET)
36     { "engine", "pollset", &nxt_pollset_engine },
37 #endif
38 
39     { "engine", "poll", &nxt_poll_engine },
40     { "engine", "select", &nxt_select_engine },
41 
42 #if (NXT_HAVE_OPENSSL)
43     { "SSL/TLS", "OpenSSL", &nxt_openssl_lib },
44     { "SSL/TLS", "openssl", &nxt_openssl_lib },
45 #endif
46 
47 #if (NXT_HAVE_GNUTLS)
48     { "SSL/TLS", "GnuTLS", &nxt_gnutls_lib },
49     { "SSL/TLS", "gnutls", &nxt_gnutls_lib },
50 #endif
51 
52 #if (NXT_HAVE_CYASSL)
53     { "SSL/TLS", "CyaSSL", &nxt_cyassl_lib },
54     { "SSL/TLS", "cyassl", &nxt_cyassl_lib },
55 #endif
56 
57 };
58 
59 
60 nxt_array_t *
nxt_services_init(nxt_mp_t * mp)61 nxt_services_init(nxt_mp_t *mp)
62 {
63     nxt_uint_t           n;
64     nxt_array_t          *services;
65     nxt_service_t        *s;
66     const nxt_service_t  *service;
67 
68     services = nxt_array_create(mp, 32, sizeof(nxt_service_t));
69 
70     if (nxt_fast_path(services != NULL)) {
71 
72         service = nxt_services;
73         n = nxt_nitems(nxt_services);
74 
75         while (n != 0) {
76             s = nxt_array_add(services);
77             if (nxt_slow_path(s == NULL)) {
78                 return NULL;
79             }
80 
81             *s = *service;
82 
83             service++;
84             n--;
85         }
86     }
87 
88     return services;
89 }
90 
91 
92 nxt_int_t
nxt_service_add(nxt_array_t * services,const nxt_service_t * service)93 nxt_service_add(nxt_array_t *services, const nxt_service_t *service)
94 {
95     nxt_uint_t     n;
96     nxt_service_t  *s;
97 
98     s = services->elts;
99     n = services->nelts;
100 
101     while (n != 0) {
102         if (nxt_strcmp(s->type, service->type) != 0) {
103             goto next;
104         }
105 
106         if (nxt_strcmp(s->name, service->name) != 0) {
107             goto next;
108         }
109 
110         nxt_thread_log_alert("service \"%s:%s\" is duplicate",
111                              service->type, service->name);
112         return NXT_ERROR;
113 
114     next:
115 
116         s++;
117         n--;
118     }
119 
120     s = nxt_array_add(services);
121     if (nxt_fast_path(s != NULL)) {
122         *s = *service;
123         return NXT_OK;
124     }
125 
126     return NXT_ERROR;
127 }
128 
129 
130 const void *
nxt_service_get(nxt_array_t * services,const char * type,const char * name)131 nxt_service_get(nxt_array_t *services, const char *type, const char *name)
132 {
133     nxt_uint_t           n;
134     const nxt_service_t  *s;
135 
136     if (services != NULL) {
137         s = services->elts;
138         n = services->nelts;
139 
140     } else {
141         s = nxt_services;
142         n = nxt_nitems(nxt_services);
143     }
144 
145     while (n != 0) {
146         if (nxt_strcmp(s->type, type) == 0) {
147 
148             if (name == NULL) {
149                 return s->service;
150             }
151 
152             if (nxt_strcmp(s->name, name) == 0) {
153                 return s->service;
154             }
155         }
156 
157         s++;
158         n--;
159     }
160 
161     nxt_thread_log_alert("service \"%s%s%s\" not found",
162                          type, (name != NULL) ? ":" : "", name);
163 
164     return NULL;
165 }
166