2 * Copyright (c) 2012, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Intel Corporation nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <murphy/common/mm.h>
36 #include <murphy/common/log.h>
38 #include <murphy-db/mqi.h>
40 #include <murphy/resource/client-api.h>
41 #include <murphy/resource/manager-api.h>
44 #include "resource-owner.h"
45 #include "resource-set.h"
46 #include "application-class.h"
50 #define RESOURCE_MAX (sizeof(mrp_resource_mask_t) * 8)
51 #define ATTRIBUTE_MAX (sizeof(mrp_attribute_mask_t) * 8)
52 #define NAME_LENGTH 24
53 #define ATTR_LENGTH 64
59 #define FIRST_ATTRIBUTE_IDX 4
62 #define VALID_TYPE(t) ((t) == mqi_string || \
63 (t) == mqi_integer || \
64 (t) == mqi_unsignd || \
72 mrp_attr_value_t attrs[MQI_COLUMN_MAX];
76 static uint32_t resource_def_count;
77 static mrp_resource_def_t *resource_def_table[RESOURCE_MAX];
78 static MRP_LIST_HOOK(manager_list);
79 static mqi_handle_t resource_user_table[RESOURCE_MAX];
81 static uint32_t add_resource_definition(const char *, bool, uint32_t,
82 mrp_resource_mgr_ftbl_t *, void *);
85 static uint32_t find_resource_attribute_id(mrp_resource_t *, const char *);
87 static mqi_data_type_t get_resource_attribute_value_type(mrp_resource_t *,
90 static mrp_attr_value_t *get_resource_attribute_default_value(mrp_resource_t*,
94 static int resource_user_create_table(mrp_resource_def_t *);
95 static void resource_user_insert(mrp_resource_t *, bool);
96 static void resource_user_delete(mrp_resource_t *);
98 static void set_attr_descriptors(mqi_column_desc_t *, mrp_resource_t *);
102 uint32_t mrp_resource_definition_create(const char *name, bool shareable,
103 mrp_attr_def_t *attrdefs,
104 mrp_resource_mgr_ftbl_t *manager,
109 mrp_resource_def_t *def;
111 MRP_ASSERT(name, "invalid argument");
113 if (mrp_resource_definition_find_by_name(name)) {
114 mrp_log_error("attmpt to redefine resource '%s'", name);
115 return MRP_RESOURCE_ID_INVALID;
118 for (nattr = 0; attrdefs && attrdefs[nattr].name; nattr++)
121 id = add_resource_definition(name, shareable, nattr, manager, mgrdata);
123 if (id != MRP_RESOURCE_ID_INVALID) {
124 def = mrp_resource_definition_find_by_id(id);
126 MRP_ASSERT(def, "got confused with data structures");
128 if (mrp_attribute_copy_definitions(attrdefs, def->attrdefs) < 0)
129 return MRP_RESOURCE_ID_INVALID;
131 resource_user_create_table(def);
132 mrp_resource_owner_create_database_table(def);
138 uint32_t mrp_resource_definition_count(void)
140 return resource_def_count;
143 mrp_resource_def_t *mrp_resource_definition_find_by_name(const char *name)
145 mrp_resource_def_t *def;
148 for (i = 0; i < resource_def_count; i++) {
149 def = resource_def_table[i];
151 if (def && !strcasecmp(name, def->name))
158 uint32_t mrp_resource_definition_get_resource_id_by_name(const char *name)
160 mrp_resource_def_t *def = mrp_resource_definition_find_by_name(name);
163 return MRP_RESOURCE_ID_INVALID;
169 mrp_resource_def_t *mrp_resource_definition_find_by_id(uint32_t id)
171 if (id < resource_def_count)
172 return resource_def_table[id];
177 mrp_resource_def_t *mrp_resource_definition_iterate_manager(void **cursor)
179 mrp_list_hook_t *entry;
181 MRP_ASSERT(cursor, "invalid argument");
183 entry = (*cursor == NULL) ? manager_list.next : (mrp_list_hook_t *)*cursor;
185 if (entry == &manager_list)
188 *cursor = entry->next;
190 return mrp_list_entry(entry, mrp_resource_def_t, manager.list);
193 const char **mrp_resource_definition_get_all_names(uint32_t buflen,
198 MRP_ASSERT(!buf || (buf && buflen > 1), "invlaid argument");
201 if (buflen < resource_def_count + 1)
205 buflen = resource_def_count + 1;
206 if (!(buf = mrp_allocz(sizeof(const char *) * buflen))) {
207 mrp_log_error("Memory alloc failure. Can't get resource names");
212 for (i = 0; i < resource_def_count; i++)
213 buf[i] = resource_def_table[i]->name;
220 mrp_attr_t *mrp_resource_definition_read_all_attributes(uint32_t resid,
224 mrp_resource_def_t *rdef = mrp_resource_definition_find_by_id(resid);
229 retval = mrp_attribute_get_all_values(buflen, buf, 0, NULL, 0);
231 retval = mrp_attribute_get_all_values(buflen, buf, rdef->nattr,
236 mrp_log_error("Memory alloc failure. Can't get all "
237 "attributes of resource definition");
245 mrp_resource_t *mrp_resource_create(const char *name,
251 mrp_resource_t *res = NULL;
252 mrp_resource_def_t *rdef;
258 MRP_ASSERT(name, "invalid argument");
260 if (!(rdef = mrp_resource_definition_find_by_name(name))) {
261 mrp_log_warning("Can't find resource definition '%s'. "
262 "No resource created", name);
265 base_size = sizeof(mrp_resource_t);
266 attr_size = sizeof(mrp_attr_value_t) * rdef->nattr;
267 total_size = base_size + attr_size;
269 if (!(res = mrp_allocz(total_size))) {
270 mrp_log_error("Memory alloc failure. Can't create "
271 "resource '%s'", name);
274 mrp_list_init(&res->list);
276 res->rsetid = rsetid;
278 res->shared = rdef->shareable ? shared : false;
280 sts = mrp_attribute_set_values(attrs, rdef->nattr,
281 rdef->attrdefs, res->attrs);
283 mrp_log_error("Memory alloc failure. No '%s' "
284 "resource created", name);
288 resource_user_insert(res, autorel);
295 void mrp_resource_destroy(mrp_resource_t *res)
297 mrp_resource_def_t *rdef;
298 mqi_data_type_t type;
304 MRP_ASSERT(rdef, "invalid_argument");
306 resource_user_delete(res);
308 mrp_list_delete(&res->list);
310 for (id = 0; id < rdef->nattr; id++) {
311 type = rdef->attrdefs[id].type;
313 if (type == mqi_string)
314 mrp_free((void *)res->attrs[id].string);
321 uint32_t mrp_resource_get_id(mrp_resource_t *res)
323 mrp_resource_def_t *def;
327 MRP_ASSERT(def, "confused with internal data structures");
331 return MRP_RESOURCE_ID_INVALID;
334 const char *mrp_resource_get_name(mrp_resource_t *res)
336 mrp_resource_def_t *def;
341 MRP_ASSERT(def && def->name, "confused with internal data structures");
346 return "<unknown resource>";
349 mrp_resource_mask_t mrp_resource_get_mask(mrp_resource_t *res)
351 mrp_resource_def_t *def;
352 mrp_resource_mask_t mask = 0;
357 MRP_ASSERT(def, "confused with internal data structures");
359 mask = (mrp_resource_mask_t)1 << def->id;
365 bool mrp_resource_is_shared(mrp_resource_t *res)
373 mrp_attr_t *mrp_resource_read_attribute(mrp_resource_t *res,
378 mrp_resource_def_t *rdef;
380 MRP_ASSERT(res, "invalid argument");
384 MRP_ASSERT(rdef, "confused with data structures");
386 retval = mrp_attribute_get_value(idx, value, rdef->nattr,
387 rdef->attrdefs, res->attrs);
390 mrp_log_error("Memory alloc failure. Can't get "
391 "resource '%s' attribute %u", rdef->name, idx);
398 mrp_attr_t *mrp_resource_read_all_attributes(mrp_resource_t *res,
403 mrp_resource_def_t *rdef;
405 MRP_ASSERT(res, "invalid argument");
409 MRP_ASSERT(rdef, "confused with data structures");
411 retval = mrp_attribute_get_all_values(nvalue, values, rdef->nattr,
412 rdef->attrdefs, res->attrs);
415 mrp_log_error("Memory alloc failure. Can't get all "
416 "attributes of resource '%s'", rdef->name);
422 int mrp_resource_write_attributes(mrp_resource_t *res, mrp_attr_t *values)
425 mrp_resource_def_t *rdef;
427 MRP_ASSERT(res && values, "invalid argument");
431 MRP_ASSERT(rdef, "confused with data structures");
433 sts = mrp_attribute_set_values(values, rdef->nattr,
434 rdef->attrdefs, res->attrs);
437 mrp_log_error("Memory alloc failure. Can't set attributes "
438 "of resource '%s'", rdef->name);
444 const char *mrp_resource_get_application_class(mrp_resource_t *res)
446 mrp_resource_set_t *rset;
447 mrp_application_class_t *class;
449 MRP_ASSERT(res, "invalid argument");
451 if (!(rset = mrp_resource_set_find_by_id(res->rsetid)))
454 if (!(class = rset->class.ptr))
460 void mrp_resource_notify(mrp_resource_t *res,
461 mrp_resource_set_t *rset,
462 mrp_resource_event_t event)
464 mrp_resource_def_t *rdef;
465 mrp_resource_mgr_ftbl_t *ftbl;
466 mrp_manager_notify_func_t notify;
467 mrp_application_class_t *class;
470 MRP_ASSERT(res && rset, "inavlid argument");
474 MRP_ASSERT(rdef, "confused with data structures");
476 if ((ftbl = rdef->manager.ftbl) &&
477 (notify = ftbl->notify) &&
478 (zone = mrp_zone_find_by_id(rset->zone)) &&
479 (class = rset->class.ptr))
481 notify(event, zone, class, res, rdef->manager.userdata);
485 int mrp_resource_print(mrp_resource_t *res, uint32_t mandatory,
486 size_t indent, char *buf, int len)
488 #define PRINT(fmt, args...) if (p<e) { p += snprintf(p, e-p, fmt , ##args); }
490 mrp_resource_def_t *rdef;
498 MRP_ASSERT(res && indent < sizeof(gap)-1 && buf,
503 MRP_ASSERT(rdef, "Confused with data structures");
508 m = ((mrp_resource_mask_t)1 << rdef->id);
510 PRINT("%s%s: 0x%02x %s %s", gap, rdef->name, m,
511 (m & mandatory) ? "mandatory":"optional ",
512 res->shared ? "shared ":"exlusive");
514 p += mrp_resource_attribute_print(res, p, e-p);
524 int mrp_resource_attribute_print(mrp_resource_t *res, char *buf, int len)
526 mrp_resource_def_t *rdef;
531 MRP_ASSERT(res && buf, "invalid argument");
535 MRP_ASSERT(rdef, "Confused with data structures");
537 return mrp_attribute_print(rdef->nattr,rdef->attrdefs,res->attrs, buf,len);
541 static uint32_t add_resource_definition(const char *name,
544 mrp_resource_mgr_ftbl_t *mgrftbl,
547 mrp_resource_def_t *def;
548 const char *dup_name;
552 MRP_ASSERT(name && nattr < ATTRIBUTE_MAX, "invalid argument");
554 if (resource_def_count >= RESOURCE_MAX) {
555 mrp_log_error("Resource table overflow. Can't add resource '%s'",name);
556 return MRP_RESOURCE_ID_INVALID;
559 size = sizeof(mrp_resource_def_t) + sizeof(mrp_attr_def_t) * nattr;
561 if (!(def = mrp_allocz(size)) || !(dup_name = mrp_strdup(name))) {
562 mrp_log_error("Memory alloc failure. Can't add resource '%s'", name);
563 return MRP_RESOURCE_ID_INVALID;
566 id = resource_def_count++;
569 def->name = dup_name;
570 def->shareable = shareable;
574 def->manager.ftbl = mrp_alloc(sizeof(mrp_resource_mgr_ftbl_t));
575 def->manager.userdata = mgrdata;
577 if (def->manager.ftbl)
578 memcpy(def->manager.ftbl, mgrftbl,sizeof(mrp_resource_mgr_ftbl_t));
580 mrp_log_error("Memory alloc failure. No manager for resource '%s'",
585 resource_def_table[id] = def;
588 mrp_list_init(&def->manager.list);
590 mrp_list_append(&manager_list, &def->manager.list);
597 static uint32_t find_resource_attribute_id(mrp_resource_t *res,
600 mrp_resource_def_t *rdef;
601 mrp_attr_def_t *adef;
604 if (res && (rdef = res->def) && attrnam) {
605 for (id = 0; id < rdef->nattr; id++) {
606 adef = rdef->attrdefs + id;
608 if (!strcasecmp(attrnam, adef->name))
613 return MRP_RESOURCE_ID_INVALID;
616 static mqi_data_type_t
617 get_resource_attribute_value_type(mrp_resource_t *res, uint32_t id)
619 mrp_resource_def_t *rdef;
621 MRP_ASSERT(res, "invalid argument");
625 MRP_ASSERT(rdef, "confused with data structures");
626 MRP_ASSERT(id < rdef->nattr, "invalid argument");
628 return rdef->attrdefs[id].type;
631 static mrp_attr_value_t *
632 get_resource_attribute_default_value(mrp_resource_t *res, uint32_t id)
634 mrp_resource_def_t *rdef;
636 MRP_ASSERT(res, "invalid argument");
640 MRP_ASSERT(rdef, "confused with data structures");
641 MRP_ASSERT(id < rdef->nattr, "invalid argument");
643 return &rdef->attrdefs[id].value;
648 static int resource_user_create_table(mrp_resource_def_t *rdef)
650 MQI_COLUMN_DEFINITION_LIST(base_coldefs,
651 MQI_COLUMN_DEFINITION( "rsetid" , MQI_UNSIGNED ),
652 MQI_COLUMN_DEFINITION( "autorel", MQI_INTEGER ),
653 MQI_COLUMN_DEFINITION( "state" , MQI_INTEGER ),
654 MQI_COLUMN_DEFINITION( "grant" , MQI_INTEGER )
657 MQI_INDEX_DEFINITION(indexdef,
658 MQI_INDEX_COLUMN( "rsetid" )
661 static bool initialized = false;
664 mqi_column_def_t coldefs[MQI_COLUMN_MAX + 1];
665 mqi_column_def_t *col;
673 for (i = 0; i < RESOURCE_MAX; i++)
674 resource_user_table[i] = MQI_HANDLE_INVALID;
678 MRP_ASSERT(sizeof(base_coldefs) < sizeof(coldefs),"too many base columns");
679 MRP_ASSERT(rdef, "invalid argument");
680 MRP_ASSERT(rdef->id < RESOURCE_MAX, "confused with data structures");
681 MRP_ASSERT(resource_user_table[rdef->id] == MQI_HANDLE_INVALID,
682 "resource user table already exist");
684 snprintf(name, sizeof(name), "%s_users", rdef->name);
685 for (p = name; (c = *p); p++) {
686 if (!isascii(c) || (!isalnum(c) && c != '_'))
690 j = MQI_DIMENSION(base_coldefs) - 1;
691 memcpy(coldefs, base_coldefs, j * sizeof(mqi_column_def_t));
693 for (i = 0; i < rdef->nattr && j < MQI_COLUMN_MAX; i++, j++) {
695 atd = rdef->attrdefs + i;
697 col->name = atd->name;
698 col->type = atd->type;
699 col->length = (col->type == mqi_string) ? ATTR_LENGTH : 0;
703 memset(coldefs + j, 0, sizeof(mqi_column_def_t));
705 table = MQI_CREATE_TABLE(name, MQI_TEMPORARY, coldefs, indexdef);
707 if (table == MQI_HANDLE_INVALID) {
708 mrp_log_error("Can't create table '%s': %s", name, strerror(errno));
712 resource_user_table[rdef->id] = table;
717 static void resource_user_insert(mrp_resource_t *res, bool autorel)
719 mrp_resource_def_t *rdef = res->def;
724 mqi_column_desc_t cdsc[FIRST_ATTRIBUTE_IDX + MQI_COLUMN_MAX + 1];
726 MRP_ASSERT(FIRST_ATTRIBUTE_IDX + rdef->nattr <= MQI_COLUMN_MAX,
727 "too many attributes for a table");
729 row.rsetid = res->rsetid;
730 row.autorel = autorel;
732 row.state = mrp_resource_no_request;
733 memcpy(row.attrs, res->attrs, rdef->nattr * sizeof(mrp_attr_value_t));
736 cdsc[i].cindex = RSETID_IDX;
737 cdsc[i].offset = MQI_OFFSET(user_row_t, rsetid);
740 cdsc[i].cindex = AUTOREL_IDX;
741 cdsc[i].offset = MQI_OFFSET(user_row_t, autorel);
744 cdsc[i].cindex = STATE_IDX;
745 cdsc[i].offset = MQI_OFFSET(user_row_t, state);
748 cdsc[i].cindex = GRANT_IDX;
749 cdsc[i].offset = MQI_OFFSET(user_row_t, grant);
751 set_attr_descriptors(cdsc + (i+1), res);
756 if ((n = MQI_INSERT_INTO(resource_user_table[rdef->id], cdsc, rows)) != 1)
757 mrp_log_error("can't insert row into resource user table");
760 static void resource_user_delete(mrp_resource_t *res)
762 static uint32_t rsetid;
764 MQI_WHERE_CLAUSE(where,
765 MQI_EQUAL( MQI_COLUMN(RSETID_IDX), MQI_UNSIGNED_VAR(rsetid) )
768 mrp_resource_def_t *rdef;
771 MRP_ASSERT(res, "invalid argument");
774 rsetid = res->rsetid;
776 if ((n = MQI_DELETE(resource_user_table[rdef->id], where)) != 1)
777 mrp_log_error("Could not delete resource user");
780 void mrp_resource_user_update(mrp_resource_t *res, int state, bool grant)
782 static uint32_t rsetid;
784 MQI_WHERE_CLAUSE(where,
785 MQI_EQUAL( MQI_COLUMN(RSETID_IDX), MQI_UNSIGNED_VAR(rsetid) )
788 mrp_resource_def_t *rdef = res->def;
792 mqi_column_desc_t cdsc[FIRST_ATTRIBUTE_IDX + MQI_COLUMN_MAX + 1];
794 rsetid = res->rsetid;
796 MRP_ASSERT(1 + rdef->nattr <= MQI_COLUMN_MAX,
797 "too many attributes for a table");
801 memcpy(row.attrs, res->attrs, rdef->nattr * sizeof(mrp_attr_value_t));
804 cdsc[i].cindex = STATE_IDX;
805 cdsc[i].offset = MQI_OFFSET(user_row_t, state);
808 cdsc[i].cindex = GRANT_IDX;
809 cdsc[i].offset = MQI_OFFSET(user_row_t, grant);
811 set_attr_descriptors(cdsc + (i+1), res);
813 if ((n = MQI_UPDATE(resource_user_table[rdef->id], cdsc,&row, where)) != 1)
814 mrp_log_error("can't update row in resource user table");
817 static void set_attr_descriptors(mqi_column_desc_t *cdsc, mrp_resource_t *res)
819 mrp_resource_def_t *rdef = res->def;
823 for (i = j = 0; j < rdef->nattr; j++) {
824 switch (rdef->attrdefs[j].type) {
825 case mqi_string: o = MQI_OFFSET(user_row_t,attrs[j].string); break;
826 case mqi_integer: o = MQI_OFFSET(user_row_t,attrs[j].integer); break;
827 case mqi_unsignd: o = MQI_OFFSET(user_row_t,attrs[j].unsignd); break;
828 case mqi_floating: o = MQI_OFFSET(user_row_t,attrs[j].floating);break;
829 default: /* skip this */ continue;
832 cdsc[i].cindex = FIRST_ATTRIBUTE_IDX + j;
846 * indent-tabs-mode: nil