pa_mod.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Presence Agent, module interface
  3. *
  4. * $Id$
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. /*
  30. * 2003-03-11 updated to the new module exports interface (andrei)
  31. * 2003-03-16 flags export parameter added (janakj)
  32. * 2004-06-08 updated to the new DB api (andrei)
  33. */
  34. #include <signal.h>
  35. #include "../../lib/srdb2/db.h"
  36. #include "../../sr_module.h"
  37. #include "../../error.h"
  38. #include "subscribe.h"
  39. #include "publish.h"
  40. #include "dlist.h"
  41. #include "pa_mod.h"
  42. #include "watcher.h"
  43. #include "rpc.h"
  44. #include "qsa_interface.h"
  45. #include "async_auth.h"
  46. #include <cds/logger.h>
  47. #include <cds/cds.h>
  48. #include <presence/qsa.h>
  49. #include "status_query.h"
  50. #include "offline_winfo.h"
  51. #include "message.h"
  52. MODULE_VERSION
  53. static int pa_mod_init(void); /* Module initialization function */
  54. static int pa_child_init(int _rank); /* Module child init function */
  55. static void pa_destroy(void); /* Module destroy function */
  56. static int subscribe_fixup(void** param, int param_no); /* domain name -> domain pointer */
  57. static void timer(unsigned int ticks, void* param); /* Delete timer for all domains */
  58. int default_expires = 3600; /* Default expires value if not present in the message (for SUBSCRIBE and PUBLISH) */
  59. int max_subscription_expiration = 3600; /* max expires value for subscribe */
  60. int max_publish_expiration = 3600; /* max expires value for subscribe */
  61. int timer_interval = 10; /* Expiration timer interval in seconds */
  62. double default_priority = 0.0; /* Default priority of presence tuple */
  63. static int default_priority_percentage = 0; /* expressed as percentage because config file grammar does not support floats */
  64. int watcherinfo_notify = 1; /* send watcherinfo notifications */
  65. /** TM bind */
  66. struct tm_binds tmb;
  67. dlg_func_t dlg_func;
  68. fill_xcap_params_func fill_xcap_params = NULL;
  69. /** database */
  70. db_con_t* pa_db = NULL; /* Database connection handle */
  71. db_func_t pa_dbf;
  72. int use_db = 1;
  73. str db_url = STR_NULL;
  74. int use_place_table = 0;
  75. #ifdef HAVE_LOCATION_PACKAGE
  76. str pa_domain = STR_NULL;
  77. #endif /* HAVE_LOCATION_PACKAGE */
  78. /* DB tables */
  79. char *presentity_table = "presentity";
  80. char *presentity_contact_table = "presentity_contact";
  81. char *presentity_notes_table = "presentity_notes";
  82. char *extension_elements_table = "presentity_extensions";
  83. char *tuple_extensions_table = "tuple_extensions";
  84. char *tuple_notes_table = "tuple_notes";
  85. char *watcherinfo_table = "watcherinfo";
  86. char *place_table = "place";
  87. char *offline_winfo_table = "offline_winfo";
  88. /* columns in DB tables */
  89. char *col_uri = "uri";
  90. char *col_pdomain = "pdomain";
  91. char *col_uid = "uid";
  92. char *col_pres_id = "pres_id"; /* common for more tables */
  93. char *col_xcap_params = "xcap_params";
  94. char *col_tupleid = "tupleid"; /* common for more tables */
  95. char *col_basic = "basic";
  96. char *col_contact = "contact";
  97. char *col_etag = "etag"; /* common for more tables */
  98. char *col_published_id = "published_id";
  99. char *col_priority = "priority";
  100. char *col_expires = "expires"; /* common for more tables */
  101. char *col_dbid = "dbid"; /* common for more tables */
  102. char *col_note = "note";
  103. char *col_lang = "lang";
  104. char *col_element = "element"; /* common for more tables */
  105. char *col_status_extension = "status_extension";
  106. char *col_s_id = "s_id";
  107. char *col_w_uri = "w_uri";
  108. char *col_package = "package";
  109. char *col_status = "status";
  110. char *col_display_name = "display_name";
  111. char *col_accepts = "accepts";
  112. char *col_event = "event";
  113. char *col_dialog = "dialog";
  114. char *col_server_contact = "server_contact";
  115. char *col_doc_index = "doc_index";
  116. char *col_watcher = "watcher";
  117. char *col_events = "events";
  118. char *col_domain = "domain";
  119. char *col_created_on = "created_on";
  120. char *col_expires_on = "expires_on";
  121. /* authorization parameters */
  122. char *auth_type_str = NULL; /* type of authorization */
  123. char *winfo_auth_type_str = "implicit"; /* type of authorization */
  124. auth_params_t pa_auth_params; /* structure filled according to parameters */
  125. auth_params_t winfo_auth_params; /* structure for watcherinfo filled according to parameters */
  126. str pres_rules_file = STR_NULL; /* filename for XCAP queries */
  127. int use_bsearch = 0;
  128. int use_location_package = 0;
  129. /* use callbacks to usrloc/??? - if 0 only pusblished information is used */
  130. int use_callbacks = 1;
  131. int use_offline_winfo = 0;
  132. int offline_winfo_timer_interval = 3600;
  133. int subscribe_to_users = 0;
  134. str pa_subscription_uri = STR_NULL;
  135. /* ignore 408 response on NOTIFY messages (don't destroy the subscription in the case of it if set */
  136. int ignore_408_on_notify = 0;
  137. /* locally generated NOTIFY is terget refresh transaction */
  138. int notify_is_refresh = 1;
  139. /*
  140. * Exported functions
  141. */
  142. static cmd_export_t cmds[]={
  143. {"handle_subscription", handle_subscription, 1, subscribe_fixup, REQUEST_ROUTE | FAILURE_ROUTE},
  144. {"handle_publish", handle_publish, 1, subscribe_fixup, REQUEST_ROUTE | FAILURE_ROUTE},
  145. {"target_online", target_online, 1, subscribe_fixup, REQUEST_ROUTE | FAILURE_ROUTE},
  146. {"check_subscription_status", check_subscription_status, 1, check_subscription_status_fix, REQUEST_ROUTE | FAILURE_ROUTE},
  147. {"store_winfo", store_offline_winfo, 1, 0, REQUEST_ROUTE | FAILURE_ROUTE},
  148. {"dump_stored_winfo", dump_offline_winfo, 2, subscribe_fixup, REQUEST_ROUTE | FAILURE_ROUTE},
  149. /* TODO: move into XCAP module? */
  150. {"authorize_message", authorize_message, 1, 0, REQUEST_ROUTE | FAILURE_ROUTE},
  151. /* FIXME: are these functions used to something by somebody */
  152. /*
  153. *
  154. {"pua_exists", pua_exists, 1, subscribe_fixup, REQUEST_ROUTE },
  155. {"pa_handle_registration", pa_handle_registration, 1, subscribe_fixup, REQUEST_ROUTE },
  156. {"existing_subscription", existing_subscription, 1, subscribe_fixup, REQUEST_ROUTE },
  157. {"mangle_pidf", mangle_pidf, 0, NULL, REQUEST_ROUTE | FAILURE_ROUTE},
  158. {"mangle_message_cpim", mangle_message_cpim, 0, NULL, REQUEST_ROUTE | FAILURE_ROUTE},*/
  159. {0, 0, 0, 0, 0}
  160. };
  161. /*
  162. * Exported parameters
  163. */
  164. static param_export_t params[]={
  165. {"default_expires", PARAM_INT, &default_expires },
  166. {"max_subscription_expiration", PARAM_INT, &max_subscription_expiration },
  167. {"max_publish_expiration", PARAM_INT, &max_publish_expiration },
  168. {"auth", PARAM_STRING, &auth_type_str }, /* type of authorization: none, implicit, xcap, ... */
  169. {"winfo_auth", PARAM_STRING, &winfo_auth_type_str }, /* type of authorization: none, implicit, xcap, ... */
  170. {"use_db", PARAM_INT, &use_db },
  171. {"use_callbacks", PARAM_INT, &use_callbacks }, /* use callbacks to usrloc/jabber ? */
  172. {"accept_internal_subscriptions", PARAM_INT, &accept_internal_subscriptions },
  173. {"watcherinfo_notify", PARAM_INT, &watcherinfo_notify }, /* accept winfo subscriptions ? */
  174. {"use_offline_winfo", PARAM_INT, &use_offline_winfo }, /* use DB for offline winfo */
  175. {"offline_winfo_expiration", PARAM_INT, &offline_winfo_expiration }, /* how long hold information in DB */
  176. {"offline_winfo_timer", PARAM_INT, &offline_winfo_timer_interval }, /* basic ticks of "offline winfo" timer */
  177. {"db_url", PARAM_STR, &db_url },
  178. {"pres_rules_file", PARAM_STR, &pres_rules_file },
  179. {"ignore_408_on_notify", PARAM_INT, &ignore_408_on_notify }, /* ignore 408 responses on NOTIFY */
  180. {"timer_interval", PARAM_INT, &timer_interval },
  181. {"default_priority_percentage", PARAM_INT, &default_priority_percentage },
  182. {"auth_rules_refresh_time", PARAM_INT, &auth_rules_refresh_time },
  183. {"async_auth_queries", PARAM_INT, &async_auth_queries },
  184. {"max_auth_requests_per_tick", PARAM_INT, &max_auth_requests_per_tick },
  185. /* experimental, undocumented */
  186. {"subscribe_to_users", PARAM_INT, &subscribe_to_users },
  187. {"pa_subscription_uri", PARAM_STR, &pa_subscription_uri },
  188. /* undocumented still (TODO) */
  189. {"presentity_table", PARAM_STRING, &presentity_table },
  190. {"presentity_contact_table", PARAM_STRING, &presentity_contact_table },
  191. {"watcherinfo_table", PARAM_STRING, &watcherinfo_table },
  192. {"offline_winfo_table", PARAM_STRING, &offline_winfo_table }, /* table with offline winfo */
  193. /* deprecated (undocumented, remove) */
  194. {"place_table", PARAM_STRING, &place_table },
  195. {"use_place_table", PARAM_INT, &use_place_table },
  196. {"use_bsearch", PARAM_INT, &use_bsearch },
  197. {"use_location_package", PARAM_INT, &use_location_package },
  198. /* db columns, undocumented */
  199. /* DB: columns for table "offline_winfo" */
  200. {"uid_column", PARAM_STRING, &col_uid },
  201. {"watcher_column", PARAM_STRING, &col_watcher },
  202. {"events_column", PARAM_STRING, &col_events },
  203. {"domain_column", PARAM_STRING, &col_domain },
  204. {"status_column", PARAM_STRING, &col_status },
  205. {"created_on_column", PARAM_STRING, &col_created_on },
  206. {"expires_on_column", PARAM_STRING, &col_expires_on },
  207. {"dbid_column", PARAM_STRING, &col_dbid },
  208. /* DB: columns for table "presentity_contact" */
  209. {"pres_id_column", PARAM_STRING, &col_pres_id },
  210. {"basic_column", PARAM_STRING, &col_basic },
  211. {"expires_column", PARAM_STRING, &col_expires },
  212. {"priority_column", PARAM_STRING, &col_priority },
  213. {"contact_column", PARAM_STRING, &col_contact },
  214. {"tupleid_column", PARAM_STRING, &col_tupleid },
  215. {"etag_column", PARAM_STRING, &col_etag },
  216. {"published_id_column", PARAM_STRING, &col_published_id },
  217. /* DB: columns for table "presentity_extensions" */
  218. /*{"dbid_column", PARAM_STRING, &col_dbid },*/
  219. /*{"pres_id_column", PARAM_STRING, &col_pres_id },*/
  220. /*{"etag_column", PARAM_STRING, &col_etag },*/
  221. {"element_column", PARAM_STRING, &col_element },
  222. /*{"expires_column", PARAM_STRING, &col_expires },*/
  223. /* DB: columns for table "presentity_notes" */
  224. /*{"dbid_column", PARAM_STRING, &col_dbid },*/
  225. /*{"pres_id_column", PARAM_STRING, &col_pres_id },*/
  226. /*{"etag_column", PARAM_STRING, &col_etag },*/
  227. {"note_column", PARAM_STRING, &col_note },
  228. {"lang_column", PARAM_STRING, &col_lang },
  229. /*{"expires_column", PARAM_STRING, &col_expires },*/
  230. /* DB: columns for table "presentity" */
  231. /*{"pres_id_column", PARAM_STRING, &col_pres_id },*/
  232. {"uri_column", PARAM_STRING, &col_uri },
  233. /*{"uid_column", PARAM_STRING, &col_uid },*/
  234. {"pdomain_column", PARAM_STRING, &col_pdomain },
  235. {"xcap_params_column", PARAM_STRING, &col_xcap_params },
  236. /* DB: columns for table "tuple_extensions" */
  237. /*{"pres_id_column", PARAM_STRING, &col_pres_id },*/
  238. /*{"tupleid_column", PARAM_STRING, &col_tupleid },*/
  239. /*{"element_column", PARAM_STRING, &col_element },*/
  240. {"status_extension_column", PARAM_STRING, &col_status_extension },
  241. /* DB: columns for table "tuple_notes" */
  242. /*{"pres_id_column", PARAM_STRING, &col_pres_id },*/
  243. /*{"tupleid_column", PARAM_STRING, &col_tupleid },*/
  244. /*{"note_column", PARAM_STRING, &col_note },*/
  245. /*{"lang_column", PARAM_STRING, &col_lang },*/
  246. /* DB: columns for table "watcherinfo" */
  247. {"w_uri_column", PARAM_STRING, &col_w_uri },
  248. {"display_name_column", PARAM_STRING, &col_display_name },
  249. {"s_id_column", PARAM_STRING, &col_s_id },
  250. {"package_column", PARAM_STRING, &col_package },
  251. /*{"status_column", PARAM_STRING, &col_status },*/
  252. {"event_column", PARAM_STRING, &col_event },
  253. /*{"expires_column", PARAM_STRING, &col_expires },*/
  254. {"accepts_column", PARAM_STRING, &col_accepts },
  255. /*{"pres_id_column", PARAM_STRING, &col_pres_id },*/
  256. {"server_contact_column", PARAM_STRING, &col_server_contact },
  257. {"dialog_column", PARAM_STRING, &col_dialog },
  258. {"doc_index_column", PARAM_STRING, &col_doc_index },
  259. {"notify_is_refresh", PARAM_INT, &notify_is_refresh },
  260. {0, 0, 0}
  261. };
  262. struct module_exports exports = {
  263. "pa",
  264. cmds, /* Exported functions */
  265. pa_rpc_methods, /* RPC methods */
  266. params, /* Exported parameters */
  267. pa_mod_init, /* module initialization function */
  268. 0, /* response function*/
  269. pa_destroy, /* destroy function */
  270. 0, /* oncancel function */
  271. pa_child_init /* per-child init function */
  272. };
  273. char* decode_mime_type(char *start, char *end, unsigned int *mime_type);
  274. static void test_mimetype_parser(void)
  275. {
  276. static struct mimetype_test {
  277. char *string;
  278. int parsed;
  279. } mimetype_tests[] = {
  280. { "application/cpim", MIMETYPE(APPLICATION,CPIM) },
  281. { "text/plain", MIMETYPE(TEXT,PLAIN) },
  282. { "message/cpim", MIMETYPE(MESSAGE,CPIM) },
  283. { "application/sdp", MIMETYPE(APPLICATION,SDP) },
  284. { "application/cpl+xml", MIMETYPE(APPLICATION,CPLXML) },
  285. { "application/pidf+xml", MIMETYPE(APPLICATION,PIDFXML) },
  286. { "application/rlmi+xml", MIMETYPE(APPLICATION,RLMIXML) },
  287. { "multipart/related", MIMETYPE(MULTIPART,RELATED) },
  288. { "application/lpidf+xml", MIMETYPE(APPLICATION,LPIDFXML) },
  289. { "application/xpidf+xml", MIMETYPE(APPLICATION,XPIDFXML) },
  290. { "application/watcherinfo+xml", MIMETYPE(APPLICATION,WATCHERINFOXML) },
  291. { "application/external-body", MIMETYPE(APPLICATION,EXTERNAL_BODY) },
  292. { "application/*", MIMETYPE(APPLICATION,ALL) },
  293. { "text/xml+msrtc.pidf", MIMETYPE(TEXT,XML_MSRTC_PIDF) },
  294. { "application/cpim-pidf+xml", MIMETYPE(APPLICATION,CPIM_PIDFXML) },
  295. { NULL, 0 }
  296. };
  297. struct mimetype_test *mt = &mimetype_tests[0];
  298. LOG(L_DBG, "Presence Agent - testing mimetype parser\n");
  299. while (mt->string) {
  300. unsigned int pmt;
  301. LOG(L_DBG, "Presence Agent - parsing mimetype %s\n", mt->string);
  302. decode_mime_type(mt->string, mt->string+strlen(mt->string), &pmt);
  303. if (pmt != mt->parsed) {
  304. LOG(L_ERR, "Parsed mimetype %s got %x expected %x\n",
  305. mt->string, pmt, mt->parsed);
  306. }
  307. mt++;
  308. }
  309. }
  310. static int set_auth_params(auth_params_t *dst, const char *auth_type_str,
  311. const char *log_str)
  312. {
  313. if (!auth_type_str) {
  314. LOG(L_ERR, "no subscription authorization type for %s given, using \'implicit\'!\n", log_str);
  315. dst->type = auth_none;
  316. return 0;
  317. }
  318. if (strcmp(auth_type_str, "xcap") == 0) {
  319. dst->type = auth_xcap;
  320. return 0;
  321. }
  322. if (strcmp(auth_type_str, "none") == 0) {
  323. dst->type = auth_none;
  324. LOG(L_WARN, "using \'none\' subscription authorization for %s!\n", log_str);
  325. return 0;
  326. }
  327. if (strcmp(auth_type_str, "implicit") == 0) {
  328. dst->type = auth_implicit;
  329. return 0;
  330. }
  331. LOG(L_ERR, "Can't resolve subscription authorization for %s type: \'%s\'."
  332. " Use one of: none, implicit, xcap.\n", log_str, auth_type_str);
  333. return -1;
  334. }
  335. static int pa_mod_init(void)
  336. {
  337. load_tm_f load_tm;
  338. bind_dlg_mod_f bind_dlg;
  339. /* SER_PROFILE_INIT - init in child (each process needs it) */
  340. test_mimetype_parser();
  341. DBG("Presence Agent - initializing\n");
  342. DBG(" ... common libraries\n");
  343. qsa_initialize();
  344. if (subscribe_to_users) {
  345. if (is_str_empty(&pa_subscription_uri)) {
  346. ERR("pa_subscription_uri must be set if set subscribe_to_users\n");
  347. return -1;
  348. }
  349. if (accept_internal_subscriptions) {
  350. ERR("impossible to have set accept_internal_subscriptions together with subscribe_to_users\n");
  351. return -1;
  352. }
  353. }
  354. /* set authorization type according to requested "auth type name"
  355. * and other (type specific) parameters */
  356. if (set_auth_params(&pa_auth_params, auth_type_str, "presence") != 0) return -1;
  357. /* set authorization type for watcherinfo
  358. * according to requested "auth type name"
  359. * and other (type specific) parameters */
  360. if (set_auth_params(&winfo_auth_params, winfo_auth_type_str, "watcher info") != 0) return -1;
  361. /* import the TM auto-loading function */
  362. if ( !(load_tm=(load_tm_f)find_export("load_tm", NO_SCRIPT, 0))) {
  363. LOG(L_ERR, "Can't import tm\n");
  364. return -1;
  365. }
  366. /* let the auto-loading function load all TM stuff */
  367. if (load_tm( &tmb )==-1) {
  368. return -1;
  369. }
  370. bind_dlg = (bind_dlg_mod_f)find_export("bind_dlg_mod", -1, 0);
  371. if (!bind_dlg) {
  372. LOG(L_ERR, "Can't import dlg\n");
  373. return -1;
  374. }
  375. if (bind_dlg(&dlg_func) != 0) {
  376. return -1;
  377. }
  378. /* Register cache timer */
  379. register_timer(timer, 0, timer_interval);
  380. /* register offline winfo timer */
  381. if (use_offline_winfo)
  382. register_timer(offline_winfo_timer, 0, offline_winfo_timer_interval);
  383. #ifdef HAVE_LOCATION_PACKAGE
  384. if (pa_domain.len == 0) {
  385. LOG(L_ERR, "pa_mod_init(): pa_domain must be specified\n");
  386. return -1;
  387. }
  388. LOG(L_DBG, "pa_mod: pa_mod=%s\n", ZSW(pa_domain.s));
  389. #endif /* HAVE_LOCATION_PACKAGE */
  390. LOG(L_DBG, "pa_mod: use_db=%d us_offline_winfo=%d db_url.s=%s\n",
  391. use_db, use_offline_winfo, ZSW(db_url.s));
  392. if (use_db || use_offline_winfo) {
  393. if (!db_url.len) {
  394. LOG(L_ERR, "pa_mod_init(): no db_url specified but DB has to be used "
  395. "(use_db=%d us_offline_winfo=%d)\n", use_db, use_offline_winfo);
  396. return -1;
  397. }
  398. if (bind_dbmod(db_url.s, &pa_dbf) < 0) { /* Find database module */
  399. LOG(L_ERR, "pa_mod_init(): Can't bind database module via url %s\n", db_url.s);
  400. return -1;
  401. }
  402. if (!DB_CAPABILITY(pa_dbf, DB_CAP_ALL)) {
  403. LOG(L_ERR, "pa_mod_init(): Database module does not implement all functions needed by the module\n");
  404. return -1;
  405. }
  406. }
  407. default_priority = ((double)default_priority_percentage) / 100.0;
  408. if (pa_qsa_interface_init() != 0) {
  409. LOG(L_CRIT, "pa_mod_init(): QSA interface initialization failed!\n");
  410. return -1;
  411. }
  412. fill_xcap_params = (fill_xcap_params_func)find_export("fill_xcap_params", 0, -1);
  413. if (async_auth_timer_init() < 0) {
  414. ERR("can't init async timer\n");
  415. return -1;
  416. }
  417. LOG(L_DBG, "pa_mod_init done\n");
  418. return 0;
  419. }
  420. /* can be called after pa_mod_init and creates new PA DB connection */
  421. db_con_t* create_pa_db_connection()
  422. {
  423. if (!(use_db || use_offline_winfo)) return NULL;
  424. if (!pa_dbf.init) return NULL;
  425. return pa_dbf.init(db_url.s);
  426. }
  427. void close_pa_db_connection(db_con_t* db)
  428. {
  429. if (db && pa_dbf.close) pa_dbf.close(db);
  430. }
  431. static int pa_child_init(int _rank)
  432. {
  433. /* Shall we use database ? */
  434. if (use_db || use_offline_winfo) { /* Yes */
  435. if (_rank==PROC_INIT || _rank==PROC_MAIN || _rank==PROC_TCP_MAIN)
  436. return 0; /* do nothing for the main or tcp_main processes */
  437. pa_db = create_pa_db_connection();
  438. if (!pa_db) {
  439. LOG(L_ERR, "ERROR: pa_child_init(%d): "
  440. "Error while connecting database\n", _rank);
  441. return -1;
  442. }
  443. }
  444. #ifdef DO_TRACE
  445. SER_PROFILE_INIT
  446. #endif
  447. return 0;
  448. }
  449. static void pa_destroy(void)
  450. {
  451. DBG("PA module cleanup\n");
  452. DBG("destroying PA module\n");
  453. DBG(" ... qsa interface\n");
  454. pa_qsa_interface_destroy();
  455. DBG(" ... pdomains\n");
  456. free_all_pdomains();
  457. if ((use_db || use_offline_winfo) && pa_db) {
  458. DBG(" ... closing db connection\n");
  459. close_pa_db_connection(pa_db);
  460. }
  461. pa_db = NULL;
  462. DBG(" ... cleaning common libs\n");
  463. qsa_cleanup();
  464. }
  465. /*
  466. * Convert char* parameter to pdomain_t* pointer
  467. */
  468. static int subscribe_fixup(void** param, int param_no)
  469. {
  470. pdomain_t* d;
  471. if (param_no == 1) {
  472. LOG(L_DBG, "subscribe_fixup: pdomain name is %s\n", (char*)*param);
  473. if (register_pdomain((char*)*param, &d) < 0) {
  474. LOG(L_ERR, "subscribe_fixup(): Error while registering domain\n");
  475. return E_UNSPEC;
  476. }
  477. *param = (void*)d;
  478. }
  479. return 0;
  480. }
  481. /*
  482. * Timer handler
  483. */
  484. static void timer(unsigned int ticks, void* param)
  485. {
  486. if (timer_all_pdomains() != 0) {
  487. LOG(L_ERR, "timer(): Error while synchronizing domains\n");
  488. }
  489. }