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
58 #define FIRST_ATTRIBUTE_IDX 4
61 #define VALID_TYPE(t) ((t) == mqi_string || \
62 (t) == mqi_integer || \
63 (t) == mqi_unsignd || \
71 mrp_attr_value_t attrs[MQI_COLUMN_MAX];
75 static uint32_t resource_def_count;
76 static mrp_resource_def_t *resource_def_table[RESOURCE_MAX];
77 static MRP_LIST_HOOK(manager_list);
78 static mqi_handle_t resource_user_table[RESOURCE_MAX];
80 static uint32_t add_resource_definition(const char *, bool, uint32_t,
81 mrp_resource_mgr_ftbl_t *, void *);
84 static uint32_t find_resource_attribute_id(mrp_resource_t *, const char *);
86 static mqi_data_type_t get_resource_attribute_value_type(mrp_resource_t *,
89 static mrp_attr_value_t *get_resource_attribute_default_value(mrp_resource_t*,
93 static int resource_user_create_table(mrp_resource_def_t *);
94 static void resource_user_insert(mrp_resource_t *, bool);
95 static void resource_user_delete(mrp_resource_t *);
97 static void set_attr_descriptors(mqi_column_desc_t *, mrp_resource_t *);
101 uint32_t mrp_resource_definition_create(const char *name, bool shareable,
102 mrp_attr_def_t *attrdefs,
103 mrp_resource_mgr_ftbl_t *manager,
108 mrp_resource_def_t *def;
110 MRP_ASSERT(name, "invalid argument");
112 if (mrp_resource_definition_find_by_name(name)) {
113 mrp_log_error("attmpt to redefine resource '%s'", name);
114 return MRP_RESOURCE_ID_INVALID;
117 for (nattr = 0; attrdefs && attrdefs[nattr].name; nattr++)
120 id = add_resource_definition(name, shareable, nattr, manager, mgrdata);
122 if (id != MRP_RESOURCE_ID_INVALID) {
123 def = mrp_resource_definition_find_by_id(id);
125 MRP_ASSERT(def, "got confused with data structures");
127 if (mrp_attribute_copy_definitions(attrdefs, def->attrdefs) < 0)
128 return MRP_RESOURCE_ID_INVALID;
130 resource_user_create_table(def);
131 mrp_resource_owner_create_database_table(def);
137 uint32_t mrp_resource_definition_count(void)
139 return resource_def_count;
142 mrp_resource_def_t *mrp_resource_definition_find_by_name(const char *name)
144 mrp_resource_def_t *def;
147 for (i = 0; i < resource_def_count; i++) {
148 def = resource_def_table[i];
150 if (def && !strcasecmp(name, def->name))
157 uint32_t mrp_resource_definition_get_resource_id_by_name(const char *name)
159 mrp_resource_def_t *def = mrp_resource_definition_find_by_name(name);
162 return MRP_RESOURCE_ID_INVALID;
168 mrp_resource_def_t *mrp_resource_definition_find_by_id(uint32_t id)
170 if (id < resource_def_count)
171 return resource_def_table[id];
176 mrp_resource_def_t *mrp_resource_definition_iterate_manager(void **cursor)
178 mrp_list_hook_t *entry;
180 MRP_ASSERT(cursor, "invalid argument");
182 entry = (*cursor == NULL) ? manager_list.next : (mrp_list_hook_t *)*cursor;
184 if (entry == &manager_list)
187 *cursor = entry->next;
189 return mrp_list_entry(entry, mrp_resource_def_t, manager.list);
192 const char **mrp_resource_definition_get_all_names(uint32_t buflen,
197 MRP_ASSERT(!buf || (buf && buflen > 1), "invlaid argument");
200 if (buflen < resource_def_count + 1)
204 buflen = resource_def_count + 1;
205 if (!(buf = mrp_allocz(sizeof(const char *) * buflen))) {
206 mrp_log_error("Memory alloc failure. Can't get resource names");
211 for (i = 0; i < resource_def_count; i++)
212 buf[i] = resource_def_table[i]->name;
219 mrp_attr_t *mrp_resource_definition_read_all_attributes(uint32_t resid,
223 mrp_resource_def_t *rdef = mrp_resource_definition_find_by_id(resid);
228 retval = mrp_attribute_get_all_values(buflen, buf, 0, NULL, 0);
230 retval = mrp_attribute_get_all_values(buflen, buf, rdef->nattr,
235 mrp_log_error("Memory alloc failure. Can't get all "
236 "attributes of resource definition");
244 mrp_resource_t *mrp_resource_create(const char *name,
250 mrp_resource_t *res = NULL;
251 mrp_resource_def_t *rdef;
257 MRP_ASSERT(name, "invalid argument");
259 if (!(rdef = mrp_resource_definition_find_by_name(name))) {
260 mrp_log_warning("Can't find resource definition '%s'. "
261 "No resource created", name);
264 base_size = sizeof(mrp_resource_t);
265 attr_size = sizeof(mrp_attr_value_t) * rdef->nattr;
266 total_size = base_size + attr_size;
268 if (!(res = mrp_allocz(total_size))) {
269 mrp_log_error("Memory alloc failure. Can't create "
270 "resource '%s'", name);
273 mrp_list_init(&res->list);
275 res->rsetid = rsetid;
277 res->shared = rdef->shareable ? shared : false;
279 sts = mrp_attribute_set_values(attrs, rdef->nattr,
280 rdef->attrdefs, res->attrs);
282 mrp_log_error("Memory alloc failure. No '%s' "
283 "resource created", name);
287 resource_user_insert(res, autorel);
294 void mrp_resource_destroy(mrp_resource_t *res)
296 mrp_resource_def_t *rdef;
297 mqi_data_type_t type;
303 MRP_ASSERT(rdef, "invalid_argument");
305 resource_user_delete(res);
307 mrp_list_delete(&res->list);
309 for (id = 0; id < rdef->nattr; id++) {
310 type = rdef->attrdefs[id].type;
312 if (type == mqi_string)
313 mrp_free((void *)res->attrs[id].string);
320 uint32_t mrp_resource_get_id(mrp_resource_t *res)
322 mrp_resource_def_t *def;
326 MRP_ASSERT(def, "confused with internal data structures");
330 return MRP_RESOURCE_ID_INVALID;
333 const char *mrp_resource_get_name(mrp_resource_t *res)
335 mrp_resource_def_t *def;
340 MRP_ASSERT(def && def->name, "confused with internal data structures");
345 return "<unknown resource>";
348 mrp_resource_mask_t mrp_resource_get_mask(mrp_resource_t *res)
350 mrp_resource_def_t *def;
351 mrp_resource_mask_t mask = 0;
356 MRP_ASSERT(def, "confused with internal data structures");
358 mask = (mrp_resource_mask_t)1 << def->id;
364 bool mrp_resource_is_shared(mrp_resource_t *res)
372 mrp_attr_t *mrp_resource_read_attribute(mrp_resource_t *res,
377 mrp_resource_def_t *rdef;
379 MRP_ASSERT(res, "invalid argument");
383 MRP_ASSERT(rdef, "confused with data structures");
385 retval = mrp_attribute_get_value(idx, value, rdef->nattr,
386 rdef->attrdefs, res->attrs);
389 mrp_log_error("Memory alloc failure. Can't get "
390 "resource '%s' attribute %u", rdef->name, idx);
397 mrp_attr_t *mrp_resource_read_all_attributes(mrp_resource_t *res,
402 mrp_resource_def_t *rdef;
404 MRP_ASSERT(res, "invalid argument");
408 MRP_ASSERT(rdef, "confused with data structures");
410 retval = mrp_attribute_get_all_values(nvalue, values, rdef->nattr,
411 rdef->attrdefs, res->attrs);
414 mrp_log_error("Memory alloc failure. Can't get all "
415 "attributes of resource '%s'", rdef->name);
421 int mrp_resource_write_attributes(mrp_resource_t *res, mrp_attr_t *values)
424 mrp_resource_def_t *rdef;
426 MRP_ASSERT(res && values, "invalid argument");
430 MRP_ASSERT(rdef, "confused with data structures");
432 sts = mrp_attribute_set_values(values, rdef->nattr,
433 rdef->attrdefs, res->attrs);
436 mrp_log_error("Memory alloc failure. Can't set attributes "
437 "of resource '%s'", rdef->name);
443 const char *mrp_resource_get_application_class(mrp_resource_t *res)
445 mrp_resource_set_t *rset;
446 mrp_application_class_t *class;
448 MRP_ASSERT(res, "invalid argument");
450 if (!(rset = mrp_resource_set_find_by_id(res->rsetid)))
453 if (!(class = rset->class.ptr))
459 void mrp_resource_notify(mrp_resource_t *res,
460 mrp_resource_set_t *rset,
461 mrp_resource_event_t event)
463 mrp_resource_def_t *rdef;
464 mrp_resource_mgr_ftbl_t *ftbl;
465 mrp_manager_notify_func_t notify;
466 mrp_application_class_t *class;
469 MRP_ASSERT(res && rset, "inavlid argument");
473 MRP_ASSERT(rdef, "confused with data structures");
475 if ((ftbl = rdef->manager.ftbl) &&
476 (notify = ftbl->notify) &&
477 (zone = mrp_zone_find_by_id(rset->zone)) &&
478 (class = rset->class.ptr))
480 notify(event, zone, class, res, rdef->manager.userdata);
484 int mrp_resource_print(mrp_resource_t *res, uint32_t mandatory,
485 size_t indent, char *buf, int len)
487 #define PRINT(fmt, args...) if (p<e) { p += snprintf(p, e-p, fmt , ##args); }
489 mrp_resource_def_t *rdef;
497 MRP_ASSERT(res && indent < sizeof(gap)-1 && buf,
502 MRP_ASSERT(rdef, "Confused with data structures");
507 m = ((mrp_resource_mask_t)1 << rdef->id);
509 PRINT("%s%s: 0x%02x %s %s", gap, rdef->name, m,
510 (m & mandatory) ? "mandatory":"optional ",
511 res->shared ? "shared ":"exlusive");
513 p += mrp_resource_attribute_print(res, p, e-p);
523 int mrp_resource_attribute_print(mrp_resource_t *res, char *buf, int len)
525 mrp_resource_def_t *rdef;
530 MRP_ASSERT(res && buf, "invalid argument");
534 MRP_ASSERT(rdef, "Confused with data structures");
536 return mrp_attribute_print(rdef->nattr,rdef->attrdefs,res->attrs, buf,len);
540 static uint32_t add_resource_definition(const char *name,
543 mrp_resource_mgr_ftbl_t *mgrftbl,
546 mrp_resource_def_t *def;
547 const char *dup_name;
551 MRP_ASSERT(name && nattr < ATTRIBUTE_MAX, "invalid argument");
553 if (resource_def_count >= RESOURCE_MAX) {
554 mrp_log_error("Resource table overflow. Can't add resource '%s'",name);
555 return MRP_RESOURCE_ID_INVALID;
558 size = sizeof(mrp_resource_def_t) + sizeof(mrp_attr_def_t) * nattr;
560 if (!(def = mrp_allocz(size)) || !(dup_name = mrp_strdup(name))) {
561 mrp_log_error("Memory alloc failure. Can't add resource '%s'", name);
562 return MRP_RESOURCE_ID_INVALID;
565 id = resource_def_count++;
568 def->name = dup_name;
569 def->shareable = shareable;
573 def->manager.ftbl = mrp_alloc(sizeof(mrp_resource_mgr_ftbl_t));
574 def->manager.userdata = mgrdata;
576 if (def->manager.ftbl)
577 memcpy(def->manager.ftbl, mgrftbl,sizeof(mrp_resource_mgr_ftbl_t));
579 mrp_log_error("Memory alloc failure. No manager for resource '%s'",
584 resource_def_table[id] = def;
587 mrp_list_init(&def->manager.list);
589 mrp_list_append(&manager_list, &def->manager.list);
596 static uint32_t find_resource_attribute_id(mrp_resource_t *res,
599 mrp_resource_def_t *rdef;
600 mrp_attr_def_t *adef;
603 if (res && (rdef = res->def) && attrnam) {
604 for (id = 0; id < rdef->nattr; id++) {
605 adef = rdef->attrdefs + id;
607 if (!strcasecmp(attrnam, adef->name))
612 return MRP_RESOURCE_ID_INVALID;
615 static mqi_data_type_t
616 get_resource_attribute_value_type(mrp_resource_t *res, uint32_t id)
618 mrp_resource_def_t *rdef;
620 MRP_ASSERT(res, "invalid argument");
624 MRP_ASSERT(rdef, "confused with data structures");
625 MRP_ASSERT(id < rdef->nattr, "invalid argument");
627 return rdef->attrdefs[id].type;
630 static mrp_attr_value_t *
631 get_resource_attribute_default_value(mrp_resource_t *res, uint32_t id)
633 mrp_resource_def_t *rdef;
635 MRP_ASSERT(res, "invalid argument");
639 MRP_ASSERT(rdef, "confused with data structures");
640 MRP_ASSERT(id < rdef->nattr, "invalid argument");
642 return &rdef->attrdefs[id].value;
647 static int resource_user_create_table(mrp_resource_def_t *rdef)
649 MQI_COLUMN_DEFINITION_LIST(base_coldefs,
650 MQI_COLUMN_DEFINITION( "rsetid" , MQI_UNSIGNED ),
651 MQI_COLUMN_DEFINITION( "autorel", MQI_INTEGER ),
652 MQI_COLUMN_DEFINITION( "state" , MQI_INTEGER ),
653 MQI_COLUMN_DEFINITION( "grant" , MQI_INTEGER )
656 MQI_INDEX_DEFINITION(indexdef,
657 MQI_INDEX_COLUMN( "rsetid" )
660 static bool initialized = false;
663 mqi_column_def_t coldefs[MQI_COLUMN_MAX + 1];
664 mqi_column_def_t *col;
672 for (i = 0; i < RESOURCE_MAX; i++)
673 resource_user_table[i] = MQI_HANDLE_INVALID;
677 MRP_ASSERT(sizeof(base_coldefs) < sizeof(coldefs),"too many base columns");
678 MRP_ASSERT(rdef, "invalid argument");
679 MRP_ASSERT(rdef->id < RESOURCE_MAX, "confused with data structures");
680 MRP_ASSERT(resource_user_table[rdef->id] == MQI_HANDLE_INVALID,
681 "resource user table already exist");
683 snprintf(name, sizeof(name), "%s_users", rdef->name);
684 for (p = name; (c = *p); p++) {
685 if (!isascii(c) || (!isalnum(c) && c != '_'))
689 j = MQI_DIMENSION(base_coldefs) - 1;
690 memcpy(coldefs, base_coldefs, j * sizeof(mqi_column_def_t));
692 for (i = 0; i < rdef->nattr && j < MQI_COLUMN_MAX; i++, j++) {
694 atd = rdef->attrdefs + i;
696 col->name = atd->name;
697 col->type = atd->type;
698 col->length = (col->type == mqi_string) ? NAME_LENGTH : 0;
702 memset(coldefs + j, 0, sizeof(mqi_column_def_t));
704 table = MQI_CREATE_TABLE(name, MQI_TEMPORARY, coldefs, indexdef);
706 if (table == MQI_HANDLE_INVALID) {
707 mrp_log_error("Can't create table '%s': %s", name, strerror(errno));
711 resource_user_table[rdef->id] = table;
716 static void resource_user_insert(mrp_resource_t *res, bool autorel)
718 mrp_resource_def_t *rdef = res->def;
723 mqi_column_desc_t cdsc[FIRST_ATTRIBUTE_IDX + MQI_COLUMN_MAX + 1];
725 MRP_ASSERT(FIRST_ATTRIBUTE_IDX + rdef->nattr <= MQI_COLUMN_MAX,
726 "too many attributes for a table");
728 row.rsetid = res->rsetid;
729 row.autorel = autorel;
731 row.state = mrp_resource_no_request;
732 memcpy(row.attrs, res->attrs, rdef->nattr * sizeof(mrp_attr_value_t));
735 cdsc[i].cindex = RSETID_IDX;
736 cdsc[i].offset = MQI_OFFSET(user_row_t, rsetid);
739 cdsc[i].cindex = AUTOREL_IDX;
740 cdsc[i].offset = MQI_OFFSET(user_row_t, autorel);
743 cdsc[i].cindex = STATE_IDX;
744 cdsc[i].offset = MQI_OFFSET(user_row_t, state);
747 cdsc[i].cindex = GRANT_IDX;
748 cdsc[i].offset = MQI_OFFSET(user_row_t, grant);
750 set_attr_descriptors(cdsc + (i+1), res);
755 if ((n = MQI_INSERT_INTO(resource_user_table[rdef->id], cdsc, rows)) != 1)
756 mrp_log_error("can't insert row into resource user table");
759 static void resource_user_delete(mrp_resource_t *res)
761 static uint32_t rsetid;
763 MQI_WHERE_CLAUSE(where,
764 MQI_EQUAL( MQI_COLUMN(RSETID_IDX), MQI_UNSIGNED_VAR(rsetid) )
767 mrp_resource_def_t *rdef;
770 MRP_ASSERT(res, "invalid argument");
773 rsetid = res->rsetid;
775 if ((n = MQI_DELETE(resource_user_table[rdef->id], where)) != 1)
776 mrp_log_error("Could not delete resource user");
779 void mrp_resource_user_update(mrp_resource_t *res, int state, bool grant)
781 static uint32_t rsetid;
783 MQI_WHERE_CLAUSE(where,
784 MQI_EQUAL( MQI_COLUMN(RSETID_IDX), MQI_UNSIGNED_VAR(rsetid) )
787 mrp_resource_def_t *rdef = res->def;
791 mqi_column_desc_t cdsc[FIRST_ATTRIBUTE_IDX + MQI_COLUMN_MAX + 1];
793 rsetid = res->rsetid;
795 MRP_ASSERT(1 + rdef->nattr <= MQI_COLUMN_MAX,
796 "too many attributes for a table");
800 memcpy(row.attrs, res->attrs, rdef->nattr * sizeof(mrp_attr_value_t));
803 cdsc[i].cindex = STATE_IDX;
804 cdsc[i].offset = MQI_OFFSET(user_row_t, state);
807 cdsc[i].cindex = GRANT_IDX;
808 cdsc[i].offset = MQI_OFFSET(user_row_t, grant);
810 set_attr_descriptors(cdsc + (i+1), res);
812 if ((n = MQI_UPDATE(resource_user_table[rdef->id], cdsc,&row, where)) != 1)
813 mrp_log_error("can't update row in resource user table");
816 static void set_attr_descriptors(mqi_column_desc_t *cdsc, mrp_resource_t *res)
818 mrp_resource_def_t *rdef = res->def;
822 for (i = j = 0; j < rdef->nattr; j++) {
823 switch (rdef->attrdefs[j].type) {
824 case mqi_string: o = MQI_OFFSET(user_row_t,attrs[j].string); break;
825 case mqi_integer: o = MQI_OFFSET(user_row_t,attrs[j].integer); break;
826 case mqi_unsignd: o = MQI_OFFSET(user_row_t,attrs[j].unsignd); break;
827 case mqi_floating: o = MQI_OFFSET(user_row_t,attrs[j].floating);break;
828 default: /* skip this */ continue;
831 cdsc[i].cindex = FIRST_ATTRIBUTE_IDX + j;
845 * indent-tabs-mode: nil