1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
5 /* Universidad de Cantabria, SPAIN */
6 /* University of York, UK */
7 /* Scuola Superiore Sant'Anna, ITALY */
8 /* Kaiserslautern University, GERMANY */
9 /* Univ. Politécnica Valencia, SPAIN */
10 /* Czech Technical University in Prague, CZECH REPUBLIC */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
19 /* FRESCOR project (FP6/2005/IST/5-034026) is funded */
20 /* in part by the European Union Sixth Framework Programme */
21 /* The European Union is not liable of any use that may be */
22 /* made of this code. */
25 /* This file is part of FORB (Frescor Object Request Broker) */
27 /* FORB is free software; you can redistribute it and/or modify it */
28 /* under terms of the GNU General Public License as published by the */
29 /* Free Software Foundation; either version 2, or (at your option) any */
30 /* later version. FORB is distributed in the hope that it will be */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
33 /* General Public License for more details. You should have received a */
34 /* copy of the GNU General Public License along with FORB; see file */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, */
36 /* Cambridge, MA 02139, USA. */
38 /* As a special exception, including FORB header files in a file, */
39 /* instantiating FORB generics or templates, or linking other files */
40 /* with FORB objects to produce an executable application, does not */
41 /* by itself cause the resulting executable application to be covered */
42 /* by the GNU General Public License. This exception does not */
43 /* however invalidate any other reasons why the executable file might be */
44 /* covered by the GNU Public License. */
45 /**************************************************************************/
49 * @author Michal Sojka <sojkam1@fel.cvut.cz>
50 * @date Sun Oct 12 16:57:01 2008
52 * @brief Implementation of basic FORB's functions.
58 #define _BSD_SOURCE /* Because of on_exit() */
62 #include <forb/config.h>
63 #include <forb/forb-internal.h>
67 #include <forb/object.h>
68 #include <forb/uuid.h>
72 #include <sys/types.h>
73 #include <ul_gavlcust.h>
75 #include <ul_logreg.h>
77 #include "discovery.h"
78 #ifdef CONFIG_FORB_PROTO_UNIX
79 #include <forb/proto_unix.h>
81 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT
82 #include <forb/proto_inet.h>
86 #include <fcb_contact_info.h>
91 #define UL_LOGL_DEF UL_LOGL_DEB
93 #define UL_LOGL_DEF UL_LOGL_ERR
95 #include "log_domains.inc"
97 extern UL_LOG_CUST(ulogd_forb);
98 static int init_ul_log(void);
100 UL_LOGREG_DOMAINS_INIT_FUNCTION(forb_logreg_domains, forb_logreg_domains_array);
104 destroy_forb_on_exit(int exitcode, void *arg)
112 forb_is_alive(forb_orb _obj, CORBA_Environment *ev)
114 ul_logdeb("%s called\n", __FUNCTION__);
117 static struct forb_forb_orb_impl forb_implementation = {
118 .is_alive = forb_is_alive,
122 * Prepares #FORB_TMP_DIR directory for use by forb
124 * @return Zero on succes, -1 on error and errno is set appropriately.
127 forb_init_tmp_dir(void)
132 ret = stat(FORB_TMP_DIR, &st);
134 if (ret == 0 && !S_ISDIR(st.st_mode)) {
135 ret = unlink(FORB_TMP_DIR);
137 } else if (ret == 0) {
140 return mkdir(FORB_TMP_DIR, 0777);
144 * Thread for execution of remote requests for a FORB object.
151 forb_execution_thread(void *arg)
155 forb_execute_object(orb);
160 void hack_register_fcb(forb_orb orb, forb_port_t *port)
162 forb_object fcb = forb_object_new(orb, &FCB_SERVER_ID, 1);
164 ul_logerr("Cannot allocate FCB reference\n");
167 forb_register_reference(fcb, fres_contract_broker_reg_name);
168 forb_object_release(fcb);
171 #define hack_register_fcb(orb)
177 forb_init(int *argc, char **argv[], const struct forb_init_attr *attr)
183 forb = forb_malloc(sizeof(*forb));
185 memset(forb, 0, sizeof(*forb));
187 /* Initialize ULUT logging facility */
189 forb_logreg_domains();
194 memset(&forb->attr, 0, sizeof(forb->attr));
197 sem_init(&forb->server_ready, 0, 0);
199 if (forb_server_id_empty(&forb->attr.fixed_server_id)) {
200 forb_server_id_init(&forb->server_id);
202 forb->server_id = forb->attr.fixed_server_id;
204 forb_server_id_to_string(forb->server_id_str, &forb->server_id,
205 sizeof(forb->server_id_str));
206 ul_logdeb("Initializing forb %s\n", forb->server_id_str);
208 if (fosa_mutex_init(&forb->request_id_mutex, 0) != 0) goto err2;
209 if (fosa_mutex_init(&forb->port_mutex, 0) != 0) goto err2;
210 forb_port_init_head(forb);
212 if (fosa_mutex_init(&forb->request_mutex, 0) != 0) goto err2;
213 forb_request_nolock_init_root_field(forb);
215 if (forb_discovery_init(forb) != 0) goto err2;
217 if (fosa_mutex_init(&forb->regref_mutex, 0) != 0) goto err2;
218 forb_regref_nolock_init_root_field(forb);
220 ret = forb_init_tmp_dir();
222 ul_logerr("Cannot prepare " FORB_TMP_DIR "\n");
226 forb_executor_prepare();
228 orb = forb_forb_orb_new(NULL, &forb_implementation, forb);
230 /* Server ID must be assigned manualy */
231 orb->server = forb->server_id;
235 /* Insert our object reference to objects tree, so that we
236 * can accept remote request to our new ORB. */
237 forb_objects_nolock_insert(forb, orb);
239 ret = fosa_thread_create(&forb->execution_thread, NULL,
240 forb_execution_thread, orb);
244 /* FIXME: I do not know how to deregister the exit handler if
245 * forb_destroy() is called manually. */
246 /* on_exit(destroy_forb_on_exit, orb); */
248 #ifdef CONFIG_FORB_PROTO_UNIX
250 forb_port_t *port = forb_malloc(sizeof(*port));
252 memset(port, 0, sizeof(*port));
253 ret = forb_unix_port_init(&port->desc, &forb->server_id);
254 if (ret) goto err_free_unix;
255 ret = forb_register_port(orb, port);
256 if (ret) goto err_free_unix; /* TODO: forb_unix_port_done() */
265 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT
267 forb_port_t *port = forb_malloc(sizeof(*port));
269 struct in_addr listen_on;
271 memset(port, 0, sizeof(*port));
272 listen_on.s_addr = INADDR_ANY;
273 ret = forb_inet_port_init(&port->desc, listen_on,
274 forb->attr.fixed_tcp_port);
275 if (ret) goto err_free_inet;
276 ret = forb_register_port(orb, port);
277 if (ret) goto err_free_inet; /* TODO: forb_inet_port_done() */
284 hack_register_fcb(orb, port);
289 err2: forb_free(forb);
293 void forb_destroy(forb_orb orb)
295 forb_t *forb = forb_object_to_forb(orb);
297 forb_regref_t *regref;
299 pthread_cancel(forb->execution_thread.pthread_id);
300 pthread_join(forb->execution_thread.pthread_id, NULL);
302 /* Unregister all registered references */
303 while ((regref = forb_regref_first(forb))) {
304 forb_unregister_reference(orb, regref->name.str);
307 ul_list_for_each_cut(forb_port, forb, port) {
308 forb_destroy_port(port);
310 forb_object_release(orb);
315 /* forb_register_interface(const struct forb_interface *interface) */
317 /* gavl_insert(&type_registry, &interface->node); */
321 * Initializes server ID variable.
323 * @param server_id Serer ID to initialize.
326 forb_server_id_init(forb_server_id *server_id)
328 forb_uuid_generate((forb_uuid_t*)server_id->uuid);
332 * Checks whether the @a object is stale. Stale object is an object
333 * reference whose server cannot be contacted to handle requests.
335 * @param object Object reference to check.
337 * @return True if the object is stale, false otherwise.
339 bool forb_object_is_stale(forb_object object)
345 remote_orb = forb_get_orb_of(object);
346 if (!remote_orb) { /* This shohuld never happen */
349 /* TODO: Check not only the ORB, but also whether the object
350 * is still registered with the remote orb. */
351 forb_orb_is_alive(remote_orb, &env);
352 if (env.major == FORB_EX_COMM_FAILURE) {
353 /* Orb is not alive */
356 if (forb_exception_occurred(&env)) {
357 ul_logerr("%s: unexpected exception: %s\n", __FUNCTION__, forb_strerror(&env));
362 forb_object_release(remote_orb);
369 * Returns object reference of forb::orb object associated with the
377 forb_get_orb_of(const forb_object obj)
381 if (forb_server_id_cmp(&obj->server, &obj->orb->server) == 0) {
382 orb = forb_object_duplicate(obj->orb);
384 orb = forb_object_new(obj->orb, &obj->server, 0);
390 * Returns server ID of an object reference.
396 forb_get_server_id(const forb_object obj, forb_server_id *dest)
405 * Return instance data registered when the object was created by
408 * @param obj Object reference
410 * @return Pointer to the registered data.
413 forb_instance_data(const forb_object obj)
415 return forb_object_instance_data(obj);
419 * Returns error message correspondings FORB exception.
421 * @param env Environemnt
423 * @return Non-NULL pointer to a string.
426 forb_strerror(CORBA_Environment *env)
429 return "Invalid environemnt";
431 #define ex(e) case FORB_EX_##e: return #e; break;
432 switch (env->major) {
453 ex(OBJECT_NOT_EXIST);
458 return "Invalid error number";
462 * Return server id of the requesting application.
464 * This function should be only called from within interface
467 * @param[in] obj Object being requested
468 * @param[out] req_source Server ID of the requesting application
471 forb_get_req_source(const forb_object obj, forb_server_id *req_source)
474 if (obj && obj->exec_req) {
475 *req_source = obj->exec_req->source;
477 memset(req_source, 0, sizeof(*req_source));
483 * Internal function for allocation of sequence bufers. Used by
484 * forb_sequence_alloc_buf().
490 * @param num_elements
493 * @return CORBA_TRUE if the allocation was sucessfull, CORBA_FALSE if
497 __forb_sequence_alloc_buf_internal(void *seq, size_t seq_size,
498 void **buf_pptr, CORBA_unsigned_long *maximum_ptr,
499 unsigned num_elements, size_t elem_size)
501 memset(seq, 0, seq_size);
502 /*(seq)._length = 0;*/
503 if (num_elements && elem_size) *buf_pptr = forb_malloc(num_elements * elem_size);
504 else *buf_pptr = NULL;
505 *maximum_ptr = *buf_pptr ? num_elements : 0;
506 return (*buf_pptr != NULL) || (num_elements == 0);
509 static FILE *forb_ul_log_file;
510 static char progname[64] = "";
513 forb_ul_log_fnc(ul_log_domain_t *domain, int level,
514 const char *format, va_list ap)
517 if(!(level&UL_LOGL_CONT)) {
519 clock_gettime(CLOCK_MONOTONIC, &now);
520 fprintf(forb_ul_log_file,"%ld.%6ld: ", now.tv_sec, now.tv_nsec/1000);
522 fprintf(forb_ul_log_file,"%s: ", progname);
523 if(domain && domain->name)
524 fprintf(forb_ul_log_file,"%s: ",domain->name);
526 vfprintf(forb_ul_log_file, format, ap);
527 fflush(forb_ul_log_file);
530 static int init_ul_log(void)
538 /* if(ul_log_output != NULL) */
541 fd = open("/proc/self/cmdline", O_RDONLY);
544 ret = read(fd, path, sizeof(path)-1);
547 s = strrchr(path, '/');
550 strncpy(progname, s, sizeof(progname)-1);
555 if((log_fname=getenv("UL_LOG_FILENAME"))!=NULL){
556 forb_ul_log_file=fopen(log_fname,"a");
558 if(forb_ul_log_file==NULL)
559 forb_ul_log_file=stderr;
561 if((s=getenv("UL_DEBUG_FLG")) != NULL){
565 ul_log_redir(forb_ul_log_fnc, flg);
567 if((s = getenv("UL_LOG_LEVELS")) != NULL)
568 ul_log_domain_arg2levels(s);
574 * Wait for the server to be ready. Internal function intended forb
579 * @return Zero on success; on error -1 is returned, and errno is set
580 * to indicate the error.
582 int forb_wait_for_server_ready(forb_orb orb)
584 forb_t *forb = forb_object_to_forb(orb);
585 return sem_wait(&forb->server_ready);
589 * Signal the the FORB core that the server is ready for accepting
592 * This function should be called at the initialization of server
593 * implementation at the time when all objects are registered with
594 * executors. All other servers in the same address space are
595 * initialized after this function is called which allows the other
596 * servers to use the services provided by the calling server.
598 * @param orb ORB object.
600 * @return Zero on success; on error -1 is returned, and errno is set
601 * to indicate the error.
603 int forb_signal_server_ready(forb_orb orb)
605 forb_t *forb = forb_object_to_forb(orb);
606 return sem_post(&forb->server_ready);