widen the name field in database to accommodate longer strings
[profile/ivi/murphy.git] / src / resource / resource.c
1 /*
2  * Copyright (c) 2012, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include <murphy/common/mm.h>
36 #include <murphy/common/log.h>
37
38 #include <murphy-db/mqi.h>
39
40 #include <murphy/resource/client-api.h>
41 #include <murphy/resource/manager-api.h>
42
43 #include "resource.h"
44 #include "resource-owner.h"
45 #include "resource-set.h"
46 #include "application-class.h"
47 #include "zone.h"
48
49
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
54
55 #define RSETID_IDX           0
56 #define AUTOREL_IDX          1
57 #define STATE_IDX            2
58 #define GRANT_IDX            3
59 #define FIRST_ATTRIBUTE_IDX  4
60
61
62 #define VALID_TYPE(t) ((t) == mqi_string  || \
63                        (t) == mqi_integer || \
64                        (t) == mqi_unsignd || \
65                        (t) == mqi_floating  )
66
67 typedef struct {
68     uint32_t          rsetid;
69     int32_t           autorel;
70     int32_t           state;
71     int32_t           grant;
72     mrp_attr_value_t  attrs[MQI_COLUMN_MAX];
73 } user_row_t;
74
75
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];
80
81 static uint32_t add_resource_definition(const char *, bool, uint32_t,
82                                         mrp_resource_mgr_ftbl_t *, void *);
83
84 #if 0
85 static uint32_t find_resource_attribute_id(mrp_resource_t *, const char *);
86
87 static mqi_data_type_t   get_resource_attribute_value_type(mrp_resource_t *,
88                                                            uint32_t);
89
90 static mrp_attr_value_t *get_resource_attribute_default_value(mrp_resource_t*,
91                                                               uint32_t);
92 #endif
93
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 *);
97
98 static void set_attr_descriptors(mqi_column_desc_t *, mrp_resource_t *);
99
100
101
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,
105                                         void *mgrdata)
106 {
107     uint32_t nattr;
108     uint32_t id;
109     mrp_resource_def_t *def;
110
111     MRP_ASSERT(name, "invalid argument");
112
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;
116     }
117
118     for (nattr = 0;  attrdefs && attrdefs[nattr].name;  nattr++)
119         ;
120
121     id = add_resource_definition(name, shareable, nattr, manager, mgrdata);
122
123     if (id != MRP_RESOURCE_ID_INVALID) {
124         def = mrp_resource_definition_find_by_id(id);
125
126         MRP_ASSERT(def, "got confused with data structures");
127
128         if (mrp_attribute_copy_definitions(attrdefs, def->attrdefs) < 0)
129             return MRP_RESOURCE_ID_INVALID;
130
131         resource_user_create_table(def);
132         mrp_resource_owner_create_database_table(def);
133     }
134
135     return id;
136 }
137
138 uint32_t mrp_resource_definition_count(void)
139 {
140     return resource_def_count;
141 }
142
143 mrp_resource_def_t *mrp_resource_definition_find_by_name(const char *name)
144 {
145     mrp_resource_def_t *def;
146     uint32_t            i;
147
148     for (i = 0;  i < resource_def_count;  i++) {
149         def = resource_def_table[i];
150
151         if (def && !strcasecmp(name, def->name))
152             return def;
153     }
154
155     return NULL;
156 }
157
158 uint32_t mrp_resource_definition_get_resource_id_by_name(const char *name)
159 {
160     mrp_resource_def_t *def = mrp_resource_definition_find_by_name(name);
161
162     if (!def) {
163         return MRP_RESOURCE_ID_INVALID;
164     }
165
166     return def->id;
167 }
168
169 mrp_resource_def_t *mrp_resource_definition_find_by_id(uint32_t id)
170 {
171     if (id < resource_def_count)
172         return resource_def_table[id];
173
174     return NULL;
175 }
176
177 mrp_resource_def_t *mrp_resource_definition_iterate_manager(void **cursor)
178 {
179     mrp_list_hook_t *entry;
180
181     MRP_ASSERT(cursor, "invalid argument");
182
183     entry = (*cursor == NULL) ? manager_list.next : (mrp_list_hook_t *)*cursor;
184
185     if (entry == &manager_list)
186         return NULL;
187
188     *cursor = entry->next;
189
190     return mrp_list_entry(entry, mrp_resource_def_t, manager.list);
191 }
192
193 const char **mrp_resource_definition_get_all_names(uint32_t buflen,
194                                                    const char **buf)
195 {
196     uint32_t i;
197
198     MRP_ASSERT(!buf || (buf && buflen > 1), "invlaid argument");
199
200     if (buf) {
201         if (buflen < resource_def_count + 1)
202             return NULL;
203     }
204     else {
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");
208             return NULL;
209         }
210     }
211
212     for (i = 0;  i < resource_def_count;  i++)
213         buf[i] = resource_def_table[i]->name;
214
215     buf[i] = NULL;
216
217     return buf;
218 }
219
220 mrp_attr_t *mrp_resource_definition_read_all_attributes(uint32_t resid,
221                                                         uint32_t buflen,
222                                                         mrp_attr_t *buf)
223 {
224     mrp_resource_def_t *rdef   = mrp_resource_definition_find_by_id(resid);
225     mrp_attr_t         *retval;
226
227
228     if (!rdef)
229         retval = mrp_attribute_get_all_values(buflen, buf, 0, NULL, 0);
230     else {
231         retval = mrp_attribute_get_all_values(buflen, buf, rdef->nattr,
232                                               rdef->attrdefs, 0);
233     }
234
235     if (!retval) {
236         mrp_log_error("Memory alloc failure. Can't get all "
237                       "attributes of resource definition");
238     }
239
240     return retval;
241 }
242
243
244
245 mrp_resource_t *mrp_resource_create(const char *name,
246                                     uint32_t    rsetid,
247                                     bool        autorel,
248                                     bool        shared,
249                                     mrp_attr_t *attrs)
250 {
251     mrp_resource_t *res = NULL;
252     mrp_resource_def_t *rdef;
253     size_t base_size;
254     size_t attr_size;
255     size_t total_size;
256     int sts;
257
258     MRP_ASSERT(name, "invalid argument");
259
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);
263     }
264     else {
265         base_size  = sizeof(mrp_resource_t);
266         attr_size  = sizeof(mrp_attr_value_t) * rdef->nattr;
267         total_size = base_size + attr_size;
268
269         if (!(res = mrp_allocz(total_size))) {
270             mrp_log_error("Memory alloc failure. Can't create "
271                           "resource '%s'", name);
272         }
273         else {
274             mrp_list_init(&res->list);
275
276             res->rsetid = rsetid;
277             res->def = rdef;
278             res->shared = rdef->shareable ?  shared : false;
279
280             sts = mrp_attribute_set_values(attrs, rdef->nattr,
281                                            rdef->attrdefs, res->attrs);
282             if (sts < 0) {
283                 mrp_log_error("Memory alloc failure. No '%s' "
284                               "resource created", name);
285                 return NULL;
286             }
287
288             resource_user_insert(res, autorel);
289         }
290     }
291
292     return res;
293 }
294
295 void mrp_resource_destroy(mrp_resource_t *res)
296 {
297     mrp_resource_def_t *rdef;
298     mqi_data_type_t type;
299     uint32_t id;
300
301     if (res) {
302         rdef = res->def;
303
304         MRP_ASSERT(rdef, "invalid_argument");
305
306         resource_user_delete(res);
307
308         mrp_list_delete(&res->list);
309
310         for (id = 0;  id < rdef->nattr;  id++) {
311             type = rdef->attrdefs[id].type;
312
313             if (type == mqi_string)
314                 mrp_free((void *)res->attrs[id].string);
315         }
316
317         mrp_free(res);
318     }
319 }
320
321 uint32_t mrp_resource_get_id(mrp_resource_t *res)
322 {
323     mrp_resource_def_t *def;
324
325     if (res) {
326         def = res->def;
327         MRP_ASSERT(def, "confused with internal data structures");
328         return def->id;
329     }
330
331     return MRP_RESOURCE_ID_INVALID;
332 }
333
334 const char *mrp_resource_get_name(mrp_resource_t *res)
335 {
336     mrp_resource_def_t *def;
337
338     if (res) {
339         def = res->def;
340
341         MRP_ASSERT(def && def->name, "confused with internal data structures");
342
343         return def->name;
344     }
345
346     return "<unknown resource>";
347 }
348
349 mrp_resource_mask_t mrp_resource_get_mask(mrp_resource_t *res)
350 {
351     mrp_resource_def_t *def;
352     mrp_resource_mask_t mask = 0;
353
354     if (res) {
355         def = res->def;
356
357         MRP_ASSERT(def, "confused with internal data structures");
358
359         mask = (mrp_resource_mask_t)1 << def->id;
360     }
361
362     return mask;
363 }
364
365 bool mrp_resource_is_shared(mrp_resource_t *res)
366 {
367     if (res)
368         return res->shared;
369
370     return false;
371 }
372
373 mrp_attr_t *mrp_resource_read_attribute(mrp_resource_t *res,
374                                         uint32_t        idx,
375                                         mrp_attr_t     *value)
376 {
377     mrp_attr_t *retval;
378     mrp_resource_def_t *rdef;
379
380     MRP_ASSERT(res, "invalid argument");
381
382     rdef = res->def;
383
384     MRP_ASSERT(rdef, "confused with data structures");
385
386     retval = mrp_attribute_get_value(idx, value, rdef->nattr,
387                                      rdef->attrdefs, res->attrs);
388
389     if (!retval) {
390         mrp_log_error("Memory alloc failure. Can't get "
391                       "resource '%s' attribute %u", rdef->name, idx);
392     }
393
394     return retval;
395 }
396
397
398 mrp_attr_t *mrp_resource_read_all_attributes(mrp_resource_t *res,
399                                              uint32_t nvalue,
400                                              mrp_attr_t *values)
401 {
402     mrp_attr_t *retval;
403     mrp_resource_def_t *rdef;
404
405     MRP_ASSERT(res, "invalid argument");
406
407     rdef = res->def;
408
409     MRP_ASSERT(rdef, "confused with data structures");
410
411     retval = mrp_attribute_get_all_values(nvalue, values, rdef->nattr,
412                                           rdef->attrdefs, res->attrs);
413
414     if (!retval) {
415         mrp_log_error("Memory alloc failure. Can't get all "
416                       "attributes of resource '%s'", rdef->name);
417     }
418
419     return retval;
420 }
421
422 int mrp_resource_write_attributes(mrp_resource_t *res, mrp_attr_t *values)
423 {
424     int sts;
425     mrp_resource_def_t *rdef;
426
427     MRP_ASSERT(res && values, "invalid argument");
428
429     rdef = res->def;
430
431     MRP_ASSERT(rdef, "confused with data structures");
432
433     sts = mrp_attribute_set_values(values, rdef->nattr,
434                                    rdef->attrdefs, res->attrs);
435
436     if (sts < 0) {
437         mrp_log_error("Memory alloc failure. Can't set attributes "
438                       "of resource '%s'", rdef->name);
439     }
440
441     return sts;
442 }
443
444 const char *mrp_resource_get_application_class(mrp_resource_t *res)
445 {
446     mrp_resource_set_t *rset;
447     mrp_application_class_t *class;
448
449     MRP_ASSERT(res, "invalid argument");
450
451     if (!(rset = mrp_resource_set_find_by_id(res->rsetid)))
452         return NULL;
453
454     if (!(class = rset->class.ptr))
455         return NULL;
456
457     return class->name;
458 }
459
460 void mrp_resource_notify(mrp_resource_t *res,
461                          mrp_resource_set_t *rset,
462                          mrp_resource_event_t event)
463 {
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;
468     mrp_zone_t *zone;
469
470     MRP_ASSERT(res && rset, "inavlid argument");
471
472     rdef = res->def;
473
474     MRP_ASSERT(rdef, "confused with data structures");
475
476     if ((ftbl = rdef->manager.ftbl) &&
477         (notify = ftbl->notify) &&
478         (zone = mrp_zone_find_by_id(rset->zone)) &&
479         (class = rset->class.ptr))
480     {
481         notify(event, zone, class, res, rdef->manager.userdata);
482     }
483 }
484
485 int mrp_resource_print(mrp_resource_t *res, uint32_t mandatory,
486                        size_t indent, char *buf, int len)
487 {
488 #define PRINT(fmt, args...)  if (p<e) { p += snprintf(p, e-p, fmt , ##args); }
489
490     mrp_resource_def_t *rdef;
491     char gap[] = "                         ";
492     char *p, *e;
493     uint32_t m;
494
495     if (len <= 0)
496         return 0;
497
498     MRP_ASSERT(res && indent < sizeof(gap)-1 && buf,
499                "invalid argument");
500
501     rdef = res->def;
502
503     MRP_ASSERT(rdef, "Confused with data structures");
504
505     gap[indent] = '\0';
506
507     e = (p = buf) + len;
508     m = ((mrp_resource_mask_t)1 << rdef->id);
509
510     PRINT("%s%s: 0x%02x %s %s", gap, rdef->name, m,
511           (m & mandatory) ? "mandatory":"optional ",
512           res->shared ? "shared  ":"exlusive");
513
514     p += mrp_resource_attribute_print(res, p, e-p);
515
516     PRINT("\n");
517
518
519     return p - buf;
520
521 #undef PRINT
522 }
523
524 int mrp_resource_attribute_print(mrp_resource_t *res, char *buf, int len)
525 {
526     mrp_resource_def_t *rdef;
527
528     if (len <= 0)
529         return 0;
530
531     MRP_ASSERT(res && buf, "invalid argument");
532
533     rdef = res->def;
534
535     MRP_ASSERT(rdef, "Confused with data structures");
536
537     return mrp_attribute_print(rdef->nattr,rdef->attrdefs,res->attrs, buf,len);
538 }
539
540
541 static uint32_t add_resource_definition(const char *name,
542                                         bool        shareable,
543                                         uint32_t    nattr,
544                                         mrp_resource_mgr_ftbl_t *mgrftbl,
545                                         void       *mgrdata)
546 {
547     mrp_resource_def_t *def;
548     const char         *dup_name;
549     size_t              size;
550     uint32_t            id;
551
552     MRP_ASSERT(name && nattr < ATTRIBUTE_MAX, "invalid argument");
553
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;
557     }
558
559     size = sizeof(mrp_resource_def_t) + sizeof(mrp_attr_def_t) * nattr;
560
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;
564     }
565
566     id = resource_def_count++;
567
568     def->id        = id;
569     def->name      = dup_name;
570     def->shareable = shareable;
571     def->nattr     = nattr;
572
573     if (mgrftbl) {
574         def->manager.ftbl = mrp_alloc(sizeof(mrp_resource_mgr_ftbl_t));
575         def->manager.userdata = mgrdata;
576
577         if (def->manager.ftbl)
578             memcpy(def->manager.ftbl, mgrftbl,sizeof(mrp_resource_mgr_ftbl_t));
579         else {
580             mrp_log_error("Memory alloc failure. No manager for resource '%s'",
581                           name);
582         }
583     }
584
585     resource_def_table[id] = def;
586
587     if (!mgrftbl)
588         mrp_list_init(&def->manager.list);
589     else
590         mrp_list_append(&manager_list, &def->manager.list);
591
592     return id;
593 }
594
595
596 #if 0
597 static uint32_t find_resource_attribute_id(mrp_resource_t *res,
598                                            const char *attrnam)
599 {
600     mrp_resource_def_t *rdef;
601     mrp_attr_def_t *adef;
602     uint32_t id;
603
604     if (res && (rdef = res->def) && attrnam) {
605         for (id = 0;  id < rdef->nattr;  id++) {
606             adef = rdef->attrdefs + id;
607
608             if (!strcasecmp(attrnam, adef->name))
609                 return id;
610         }
611     }
612
613     return MRP_RESOURCE_ID_INVALID;
614 }
615
616 static mqi_data_type_t
617 get_resource_attribute_value_type(mrp_resource_t *res, uint32_t id)
618 {
619     mrp_resource_def_t *rdef;
620
621     MRP_ASSERT(res, "invalid argument");
622
623     rdef = res->def;
624
625     MRP_ASSERT(rdef, "confused with data structures");
626     MRP_ASSERT(id < rdef->nattr, "invalid argument");
627
628     return rdef->attrdefs[id].type;
629 }
630
631 static mrp_attr_value_t *
632 get_resource_attribute_default_value(mrp_resource_t *res, uint32_t id)
633 {
634     mrp_resource_def_t *rdef;
635
636     MRP_ASSERT(res, "invalid argument");
637
638     rdef = res->def;
639
640     MRP_ASSERT(rdef, "confused with data structures");
641     MRP_ASSERT(id < rdef->nattr, "invalid argument");
642
643     return &rdef->attrdefs[id].value;
644 }
645 #endif
646
647
648 static int resource_user_create_table(mrp_resource_def_t *rdef)
649 {
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  )
655     );
656
657     MQI_INDEX_DEFINITION(indexdef,
658         MQI_INDEX_COLUMN( "rsetid" )
659     );
660
661     static bool initialized = false;
662
663     char name[256];
664     mqi_column_def_t  coldefs[MQI_COLUMN_MAX + 1];
665     mqi_column_def_t *col;
666     mrp_attr_def_t *atd;
667     mqi_handle_t table;
668     char c, *p;
669     size_t i,j;
670
671     if (!initialized) {
672         mqi_open();
673         for (i = 0;  i < RESOURCE_MAX;  i++)
674             resource_user_table[i] = MQI_HANDLE_INVALID;
675         initialized = true;
676     }
677
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");
683
684     snprintf(name, sizeof(name), "%s_users", rdef->name);
685     for (p = name; (c = *p);  p++) {
686         if (!isascii(c) || (!isalnum(c) && c != '_'))
687             *p = '_';
688     }
689
690     j = MQI_DIMENSION(base_coldefs) - 1;
691     memcpy(coldefs, base_coldefs, j * sizeof(mqi_column_def_t));
692
693     for (i = 0;  i < rdef->nattr && j < MQI_COLUMN_MAX;  i++, j++) {
694         col = coldefs + j;
695         atd = rdef->attrdefs + i;
696
697         col->name   = atd->name;
698         col->type   = atd->type;
699         col->length = (col->type == mqi_string) ? ATTR_LENGTH : 0;
700         col->flags  = 0;
701     }
702
703     memset(coldefs + j, 0, sizeof(mqi_column_def_t));
704
705     table = MQI_CREATE_TABLE(name, MQI_TEMPORARY, coldefs, indexdef);
706
707     if (table == MQI_HANDLE_INVALID) {
708         mrp_log_error("Can't create table '%s': %s", name, strerror(errno));
709         return -1;
710     }
711
712     resource_user_table[rdef->id] = table;
713
714     return 0;
715 }
716
717 static void resource_user_insert(mrp_resource_t *res, bool autorel)
718 {
719     mrp_resource_def_t *rdef = res->def;
720     uint32_t i;
721     int n;
722     user_row_t row;
723     user_row_t *rows[2];
724     mqi_column_desc_t cdsc[FIRST_ATTRIBUTE_IDX + MQI_COLUMN_MAX + 1];
725
726     MRP_ASSERT(FIRST_ATTRIBUTE_IDX + rdef->nattr <= MQI_COLUMN_MAX,
727                "too many attributes for a table");
728
729     row.rsetid   = res->rsetid;
730     row.autorel  = autorel;
731     row.grant    = 0;
732     row.state    = mrp_resource_no_request;
733     memcpy(row.attrs, res->attrs, rdef->nattr * sizeof(mrp_attr_value_t));
734
735     i = 0;
736     cdsc[i].cindex = RSETID_IDX;
737     cdsc[i].offset = MQI_OFFSET(user_row_t, rsetid);
738
739     i++;
740     cdsc[i].cindex = AUTOREL_IDX;
741     cdsc[i].offset = MQI_OFFSET(user_row_t, autorel);
742
743     i++;
744     cdsc[i].cindex = STATE_IDX;
745     cdsc[i].offset = MQI_OFFSET(user_row_t, state);
746
747     i++;
748     cdsc[i].cindex = GRANT_IDX;
749     cdsc[i].offset = MQI_OFFSET(user_row_t, grant);
750
751     set_attr_descriptors(cdsc + (i+1), res);
752
753     rows[0] = &row;
754     rows[1] = NULL;
755
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");
758 }
759
760 static void resource_user_delete(mrp_resource_t *res)
761 {
762     static uint32_t rsetid;
763
764     MQI_WHERE_CLAUSE(where,
765         MQI_EQUAL( MQI_COLUMN(RSETID_IDX), MQI_UNSIGNED_VAR(rsetid) )
766     );
767
768     mrp_resource_def_t *rdef;
769     int n;
770
771     MRP_ASSERT(res, "invalid argument");
772
773     rdef = res->def;
774     rsetid = res->rsetid;
775
776     if ((n = MQI_DELETE(resource_user_table[rdef->id], where)) != 1)
777         mrp_log_error("Could not delete resource user");
778 }
779
780 void mrp_resource_user_update(mrp_resource_t *res, int state, bool grant)
781 {
782     static uint32_t rsetid;
783
784     MQI_WHERE_CLAUSE(where,
785         MQI_EQUAL( MQI_COLUMN(RSETID_IDX), MQI_UNSIGNED_VAR(rsetid) )
786     );
787
788     mrp_resource_def_t *rdef = res->def;
789     uint32_t i;
790     int n;
791     user_row_t row;
792     mqi_column_desc_t cdsc[FIRST_ATTRIBUTE_IDX + MQI_COLUMN_MAX + 1];
793
794     rsetid = res->rsetid;
795
796     MRP_ASSERT(1 + rdef->nattr <= MQI_COLUMN_MAX,
797                "too many attributes for a table");
798
799     row.state = state;
800     row.grant = grant;
801     memcpy(row.attrs, res->attrs, rdef->nattr * sizeof(mrp_attr_value_t));
802
803     i = 0;
804     cdsc[i].cindex = STATE_IDX;
805     cdsc[i].offset = MQI_OFFSET(user_row_t, state);
806
807     i++;
808     cdsc[i].cindex = GRANT_IDX;
809     cdsc[i].offset = MQI_OFFSET(user_row_t, grant);
810
811     set_attr_descriptors(cdsc + (i+1), res);
812
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");
815 }
816
817 static void set_attr_descriptors(mqi_column_desc_t *cdsc, mrp_resource_t *res)
818 {
819     mrp_resource_def_t *rdef = res->def;
820     uint32_t i,j;
821     int o;
822
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;
830         }
831
832         cdsc[i].cindex = FIRST_ATTRIBUTE_IDX + j;
833         cdsc[i].offset = o;
834         i++;
835     }
836
837     cdsc[i].cindex = -1;
838     cdsc[i].offset =  1;
839 }
840
841
842
843 /*
844  * Local Variables:
845  * c-basic-offset: 4
846  * indent-tabs-mode: nil
847  * End:
848  *
849  */