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() */
63 #include <forb/config.h>
64 #include <forb/forb-internal.h>
68 #include <forb/object.h>
69 #include <forb/uuid.h>
74 #include <sys/types.h>
75 #include <ul_gavlcust.h>
77 #include <ul_logreg.h>
79 #ifdef CONFIG_FORB_PROTO_UNIX
80 #include <forb/proto_unix.h>
84 #define UL_LOGL_DEF UL_LOGL_DEB
86 #define UL_LOGL_DEF UL_LOGL_ERR
88 #include "log_domains.inc"
90 extern UL_LOG_CUST(ulogd_forb);
94 destroy_forb_on_exit(int exitcode, void *arg)
101 forb_is_alive(forb_orb _obj, CORBA_Environment *ev)
103 ul_logdeb("%s called\n", __FUNCTION__);
106 static struct forb_forb_orb_impl forb_implementation = {
107 .is_alive = forb_is_alive,
111 * Prepares #FORB_TMP_DIR directory for use by forb
113 * @return Zero on succes, -1 on error and errno is set appropriately.
116 forb_init_tmp_dir(void)
121 ret = stat(FORB_TMP_DIR, &st);
123 if (ret == 0 && !S_ISDIR(st.st_mode)) {
124 ret = unlink(FORB_TMP_DIR);
126 } else if (ret == 0) {
129 return mkdir(FORB_TMP_DIR, 0777);
133 * Thread for execution of remote requests for a FORB object.
140 forb_execution_thread(void *arg)
144 forb_execute_object(orb);
149 forb_init(int *argc, char **argv[], const struct forb_init_attr *attr)
155 forb = forb_malloc(sizeof(*forb));
157 memset(forb, 0, sizeof(*forb));
159 /* Initialize ULUT logging facility */
160 ul_logreg_domains_static(ul_log_domains_array,
161 sizeof(ul_log_domains_array)/sizeof(ul_log_domains_array[0]));
166 memset(&forb->attr, 0, sizeof(forb->attr));
169 forb_server_id_init(&forb->server_id);
170 forb_server_id_to_string(forb->server_id_str, &forb->server_id,
171 sizeof(forb->server_id_str));
172 ul_logdeb("Initializing forb %s\n", forb->server_id_str);
174 if (fosa_mutex_init(&forb->request_id_mutex, 0) != 0) goto err2;
175 if (fosa_mutex_init(&forb->port_mutex, 0) != 0) goto err2;
176 forb_port_init_head(forb);
178 if (fosa_mutex_init(&forb->request_mutex, 0) != 0) goto err2;
179 forb_request_nolock_init_root_field(forb);
181 if (fosa_mutex_init(&forb->peer_mutex, 0) != 0) goto err2;
182 forb_peer_nolock_init_root_field(forb);
184 if (fosa_mutex_init(&forb->objkey_mutex, 0) != 0) goto err2;
185 forb_objects_nolock_init_root_field(forb);
187 if (fosa_mutex_init(&forb->regref_mutex, 0) != 0) goto err2;
188 forb_regref_nolock_init_root_field(forb);
190 ret = forb_init_tmp_dir();
192 ul_logerr("Cannot prepare " FORB_TMP_DIR "\n");
196 orb = forb_forb_orb_new(NULL, &forb_implementation, forb);
198 /* Server ID must be assigned manualy */
199 orb->server = forb->server_id;
203 /* Insert our object reference to objects tree, so that we
204 * can accept remote request to our new ORB. */
205 forb_objects_nolock_insert(forb, orb);
207 ret = fosa_thread_create(&forb->execution_thread, NULL,
208 forb_execution_thread, orb);
212 on_exit(destroy_forb_on_exit, orb);
214 #ifdef CONFIG_FORB_PROTO_UNIX
216 forb_port_t *port = forb_malloc(sizeof(*port));
218 memset(port, 0, sizeof(*port));
219 ret = forb_unix_port_init(&port->desc, &forb->server_id);
220 if (ret) goto err_free_unix;
221 ret = forb_register_port(orb, port);
222 if (ret) goto err_free_unix; /* TODO: forb_unix_port_done() */
233 err2: forb_free(forb);
237 /* FIXME: forb_destroy is now called automatically on exit. Therefore
238 * forb_destroy() should be either static or should deregister on_exit
239 * handler (how???). */
240 void forb_destroy(forb_orb orb)
242 forb_t *forb = forb_object_to_forb(orb);
244 forb_regref_t *regref;
246 pthread_cancel(forb->execution_thread.pthread_id);
247 pthread_join(forb->execution_thread.pthread_id, NULL);
249 /* Unregister all registered references */
250 while ((regref = forb_regref_first(forb))) {
251 forb_unregister_reference(orb, regref->name.str);
254 ul_list_for_each_cut(forb_port, forb, port) {
255 forb_destroy_port(port);
257 forb_object_release(orb);
262 /* forb_register_interface(const struct forb_interface *interface) */
264 /* gavl_insert(&type_registry, &interface->node); */
268 * Initializes server ID variable.
270 * @param server_id Serer ID to initialize.
273 forb_server_id_init(forb_server_id *server_id)
275 forb_uuid_generate((forb_uuid_t*)server_id->uuid);
279 * Checks whether the @a object is stale. Stale object is an object
280 * reference whose server cannot be contacted to handle requests.
282 * @param object Object reference to check.
284 * @return True if the object is stale, false otherwise.
286 bool forb_object_is_stale(forb_object object)
292 remote_orb = forb_get_orb_of(object);
293 if (!remote_orb) { /* This shohuld never happen */
296 /* TODO: Check not only the ORB, but also whether the object
297 * is still registered with the remote orb. */
298 forb_orb_is_alive(remote_orb, &env);
299 if (env.major == FORB_EX_COMM_FAILURE) {
300 /* Orb is not alive */
303 if (forb_exception_occurred(&env)) {
304 ul_logerr("%s: unexpected exception: %s\n", __FUNCTION__, forb_strerror(&env));
309 forb_object_release(remote_orb);
320 * @return Zero on success, -1 on error.
323 rewrite_regref(int fd, const char *objref)
326 int len = strlen(objref);
327 lseek(fd, 0, SEEK_SET);
331 ret = write(fd, objref, len);
332 if (ret < 0) goto out;
336 ret = 0; /* Success */
345 * @param fn File name
346 * @param objref string form of the object reference
348 * @return -1 on error, 0 if reference was replaced and 1 in the
349 * reference is valid.
352 replace_regref_if_stale(forb_orb orb, const char *fn, const char *objref)
358 fd = open(fn, O_RDWR);
363 ret = lockf(fd, F_LOCK, 0);
364 if (ret < 0) goto close_err;
365 ret = read(fd, str, sizeof(str)-1);
366 if (ret < 0) goto unlock_err;
367 /* TODO: Check that we have read the whole file */
370 object = forb_string_to_object(orb, str);
372 /* reference is unreadable, so we can replace it */
373 ret = rewrite_regref(fd, objref);
374 /* We are done for now */
377 if (forb_object_is_stale(object)) {
378 /* Orb is not alive */
379 ret = rewrite_regref(fd, objref);
381 /* Reference's FORB is alive :-( */
385 forb_object_release(object);
387 lockf(fd, F_ULOCK, 0);
396 * Registers a given object reference so that other FORBs on the same
397 * node can find it by using forb_resolve_reference().
399 * @param object Object reference to register.
400 * @param name Name under which to register the reference.
402 * @return Zero on success, -1 on error.
405 forb_register_reference(forb_object object, const char *name)
407 forb_regref_t *regref;
408 forb_regref_name_t regname;
409 forb_t *forb = forb_object_to_forb(object);
410 char fn[100], fntmp[100];
415 strncpy(regname.str, name, FORB_REGREF_NAME_LEN-1);
416 regname.str[FORB_REGREF_NAME_LEN-1]='\0';
418 regref = forb_regref_new(object, regname);
419 if (!regref) goto err;
421 forb_regref_insert(forb, regref);
423 snprintf(fn, sizeof(fn), FORB_TMP_DIR "/%s", regname.str);
424 snprintf(fntmp, sizeof(fntmp), FORB_TMP_DIR "/%s.%s", regname.str,
425 forb->server_id_str);
427 f = fopen(fntmp, "w");
428 if (!f) goto unalloc_err;
430 objref = forb_object_to_string(object);
431 if (!objref) goto unlink_err;
433 ret = fprintf(f, "%s", objref);
434 if (ret < 0) goto free_objref_err;
437 if (ret == EOF) goto free_objref_err;
439 /* Make the atomic registration in filesystem */
440 ret = link(fntmp, fn);
442 if (ret < 0 && errno == EEXIST) {
443 /* The reference exists. Try whether it is still
445 if (replace_regref_if_stale(object->orb, fn, objref) != 0) {
446 goto free_objref_err;
448 ul_logdeb("Stale registration replaced\n");
449 } else if (ret < 0) {
450 goto free_objref_err;
455 /* Unlink the temporary filename */
469 forb_regref_delete(forb, regref);
470 forb_regref_release(regref);
477 * Unregister reference previously registered by
478 * forb_register_reference().
480 * @param orb forb::orb reference
481 * @param name Name to unregister.
483 * @return Zero on success, -1 on error.
486 forb_unregister_reference(forb_orb orb, const char *name)
488 forb_regref_t *regref;
489 forb_regref_name_t regname;
490 forb_t *forb = forb_object_to_forb(orb);
493 strncpy(regname.str, name, FORB_REGREF_NAME_LEN-1);
494 regname.str[FORB_REGREF_NAME_LEN-1]='\0';
496 regref = forb_regref_find(forb, ®name);
497 if (!regref) goto err;
499 forb_regref_delete(forb, regref);
500 forb_regref_release(regref);
502 snprintf(fn, sizeof(fn), FORB_TMP_DIR "/%s", regname.str);
511 * Returns a named reference previously registered by
512 * forb_register_reference(). This function can be called even if the
513 * reference was registered in a diferent process (but on the same
516 * @param orb Local orb object reference
517 * @param name Name under which the reference was registered.
519 * @return Object reference on NULL in case of error.
522 forb_resolve_reference(const forb_orb orb, const char *name)
524 forb_regref_name_t regname;
530 strncpy(regname.str, name, FORB_REGREF_NAME_LEN-1);
531 regname.str[FORB_REGREF_NAME_LEN-1]='\0';
533 snprintf(fn, sizeof(fn), FORB_TMP_DIR "/%s", regname.str);
539 ret = read(fd, str, sizeof(str)-1);
540 /* TODO: Check that we have read the whole file */
542 object = forb_string_to_object(orb, str);
545 if (forb_object_is_stale(object)) {
546 forb_object_release(object);
554 * Returns object reference of forb::orb object associated with the
562 forb_get_orb_of(const forb_object obj)
566 if (forb_server_id_cmp(&obj->server, &obj->orb->server) == 0) {
567 orb = forb_object_duplicate(obj->orb);
569 orb = forb_object_new(obj->orb, &obj->server, 0);
575 * Returns server ID of an object reference.
581 forb_get_server_id(const forb_object obj, forb_server_id *dest)
590 * Return instance data registered when the object was created by
593 * @param obj Object reference
595 * @return Pointer to the registered data.
598 forb_instance_data(const forb_object obj)
600 return forb_object_instance_data(obj);
604 * Returns error message correspondings FORB exception.
606 * @param env Environemnt
608 * @return Non-NULL pointer to a string.
611 forb_strerror(CORBA_Environment *env)
614 return "Invalid environemnt";
616 #define ex(e) case FORB_EX_##e: return #e; break;
617 switch (env->major) {
638 ex(OBJECT_NOT_EXIST);
643 return "Invalid error number";
647 * Return server id of the requesting application.
649 * This function should be only called from within interface
652 * @param[in] obj Object being requested
653 * @param[out] req_source Server ID of the requesting application
656 forb_get_req_source(const forb_object obj, forb_server_id *req_source)
659 if (obj && obj->exec_req) {
660 *req_source = obj->exec_req->source;
662 memset(req_source, 0, sizeof(*req_source));