]> andersk Git - moira.git/blob - server/qrtn.qc
Detect Ingres Deadlock & return an SMS error
[moira.git] / server / qrtn.qc
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987, 1988 by the Massachusetts Institute of Technology
7  * 
8  */
9
10 #ifndef lint
11 static char *rcsid_qrtn_qc = "$Header$";
12 #endif lint
13
14 #include "query.h"
15 #include "sms_server.h"
16
17 char *Argv[16];
18
19 static int ingres_errno = 0;
20 extern char *whoami;
21 extern FILE *journal;
22
23 #define INGRES_BAD_INT 4111
24 #define INGRES_BAD_DATE 4302
25 #define INGRES_DEADLOCK 4700
26
27 /*
28  * ingerr: (supposedly) called when Ingres indicates an error.
29  * I have not yet been able to get this to work to intercept a
30  * database open error.
31  */
32
33 static int ingerr(num)
34     int *num;
35 {
36     char buf[256];
37
38     switch (*num) {
39     case INGRES_BAD_INT:
40         ingres_errno = SMS_INTEGER;
41         break;
42     case INGRES_BAD_DATE:
43         ingres_errno = SMS_DATE;
44         break;
45     case INGRES_DEADLOCK:
46         ingres_errno = SMS_DEADLOCK;
47         break;
48     default:
49         ingres_errno = SMS_INGRES_ERR;
50         com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
51         sprintf(buf, "Ingres error %d", *num);
52         send_zgram("ingres_error", buf);
53         return (*num);
54     }
55     return (0);
56 }
57
58 int sms_open_database()
59 {
60     register int i;
61     char *malloc();
62
63     /* initialize local argv */
64     for (i = 0; i < 16; i++)
65         Argv[i] = malloc(ARGLEN);
66
67     IIseterr(ingerr);
68         
69     ingres_errno = 0;
70         
71     /* open the database */
72 ##  ingres sms
73     return ingres_errno;
74 }
75
76 int sms_close_database()
77 {
78 ##  exit
79 }
80
81 sms_check_access(cl, name, argc, argv_ro)
82     client *cl;
83     char *name;
84     int argc;
85     char *argv_ro[];
86 {
87     struct query *q;
88     struct query *get_query_by_name();
89
90     q = get_query_by_name(name, cl->args->sms_version_no);
91     if (q == (struct query *)0)
92         return(SMS_NO_HANDLE);
93
94     return(sms_verify_query(cl, q, argc, argv_ro));
95 }
96
97 sms_process_query(cl, name, argc, argv_ro, action, actarg)
98     client *cl;
99     char *name;
100     int argc;
101     char *argv_ro[];
102     int (*action)();
103     char *actarg;
104 {
105     register struct query *q;
106     register int status;
107     register struct validate *v;
108     char qual[256];
109     char sort[32];
110     char *pqual;
111     char *psort;
112 ##  char *table;
113     struct save_queue *sq;
114     struct query *get_query_by_name();
115     int sq_save_args();
116     struct save_queue *sq_create();
117     char *build_sort();
118
119     /* list queries command */
120     if (!strcmp(name, "_list_queries")) {
121         list_queries(cl->args->sms_version_no, action, actarg);
122         return(SMS_SUCCESS);
123     }
124
125     /* help query command */
126     if (!strcmp(name, "_help")) {
127         if (argc < 1)
128             return(SMS_ARGS);
129         q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
130         if (q == (struct query *)0) return(SMS_NO_HANDLE);
131         help_query(q, action, actarg);
132         return(SMS_SUCCESS);
133     }
134
135     /* get query structure, return error if named query does not exist */
136     q = get_query_by_name(name, cl->args->sms_version_no);
137     if (q == (struct query *)0) return(SMS_NO_HANDLE);
138     v = q->validate;
139
140     if (q->type != RETRIEVE)
141 ##      begin transaction
142
143     /* setup argument vector, verify access and arguments */
144     if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
145         goto out;
146
147     /* perform any special query pre-processing */
148     if (v && v->pre_rtn) {
149         status = (*v->pre_rtn)(q, Argv, cl, 0);
150         if (status != SMS_SUCCESS)
151             goto out;
152     }
153
154     switch (q->type) {
155     case RETRIEVE:
156         /* for queries that do not permit wildcarding, check if row
157            uniquely exists */
158         if (v && v->field) {
159             status = validate_row(q, Argv, v);
160             if (status != SMS_EXISTS) break;
161         }
162
163         /* build "where" clause if needed */
164         if (q->qual) {
165             build_qual(q->qual, q->argc, Argv, qual);
166             pqual = qual;
167         } else {
168             pqual = 0;
169         }
170
171         /* build "sort" clause if needed */
172         if (v && v->valobj) {
173             psort = build_sort(v, sort);
174         } else {
175             psort = 0;
176         }
177
178         /* if there is a followup routine, then we must save the results */
179         /* of the first query for use by the followup routine */
180         /* if q->rvar = NULL, perform post_rtn only */
181         if (q->rvar) {
182             if (v && v->post_rtn) {
183                 sq = sq_create();
184                 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
185                 if (status != SMS_SUCCESS) {
186                     sq_destroy(sq);
187                     break;
188                 }
189                 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
190             } else {
191                 /* normal retrieve */
192                 status = do_retrieve(q, pqual, psort, action, actarg);
193             }
194             if (status != SMS_SUCCESS) break;
195             table = q->rtable;
196 ##          repeat replace tblstats (retrieves = tblstats.retrieves + 1)
197 ##                 where tblstats.#table = @table
198         } else {
199             status = (*v->post_rtn)(q, Argv, cl, action, actarg);
200         }
201
202         break;
203
204     case UPDATE:
205         /* see if row already exists */
206         if (v->field) {
207             status = validate_row(q, Argv, v);
208             if (status != SMS_EXISTS) break;
209         }
210
211         /* build "where" clause and perform update */
212         /* if q->rvar = NULL, perform post_rtn only */
213         if (q->rvar) {
214             build_qual(q->qual, q->argc, Argv, qual);
215             status = do_update(q, &Argv[q->argc], qual, action, actarg);
216             if (status != SMS_SUCCESS) break;
217             table = q->rtable;
218 ##          repeat replace tblstats (updates = tblstats.updates + 1,
219 ##                                   modtime = "now")
220 ##              where tblstats.#table = @table
221         }
222
223         /* execute followup routine (if any) */
224         if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
225
226         break;
227
228     case APPEND:
229         /* see if row already exists */
230         if (v->field) {
231             status = validate_row(q, Argv, v);
232             if (status != SMS_NO_MATCH) break;
233         }
234
235         /* increment id number if necessary */
236         if (v->object_id) {
237             status = set_next_object_id(v->object_id, q->rtable);
238             if (status != SMS_SUCCESS) break;
239         }
240
241         /* build "where" clause if needed */
242         if (q->qual) {
243             build_qual(q->qual, q->argc, Argv, qual);
244             pqual = qual;
245         } else {
246             pqual = 0;
247         }
248
249         /* perform the append */
250         /* if q->rvar = NULL, perform post_rtn only */
251         if (q->rvar) {
252             status = do_append(q, &Argv[q->argc], pqual, action, actarg);
253             if (status != SMS_SUCCESS) break;
254             table = q->rtable;
255 ##          repeat replace tblstats (appends = tblstats.appends + 1,
256 ##                                   modtime = "now")
257 ##              where tblstats.#table = @table
258         }
259         
260         /* execute followup routine */
261         if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
262         break;
263
264     case DELETE:
265         /* see if row already exists */
266         if (v->field) {
267             status = validate_row(q, Argv, v);
268             if (status != SMS_EXISTS) break;
269         }
270
271         /* build "where" clause and perform delete */
272         /* if q->rvar = NULL, perform post_rtn only */
273         if (q->rvar) {
274             build_qual(q->qual, q->argc, Argv, qual);
275             status = do_delete(q, qual, action, actarg);
276             if (status != SMS_SUCCESS) break;
277             table = q->rtable;
278 ##          repeat replace tblstats (deletes = tblstats.deletes + 1,
279 ##                                   modtime = "now")
280 ##              where tblstats.#table = @table
281         }
282
283         /* execute followup routine */
284         if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
285         break;
286
287     }
288
289 out:
290     if (q->type != RETRIEVE) {
291         if (status == SMS_SUCCESS) {
292 ##          end transaction     /* commit to this */
293             if (journal) {
294                 char buf[1024], *bp;
295                 int i;
296                 extern time_t now;
297
298                 fprintf(journal, "%% %s %s %s",
299                         cl->clname, cl->entity, ctime(&now));
300                 fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
301                 for (i = 0; i < argc; i++) {
302                     if (i != 0) {
303                         putc(' ', journal);
304                     }
305                     requote(buf, argv_ro[i], sizeof(buf));
306                     fputs(buf, journal);
307                 }
308                 putc('\n', journal);
309                 fflush(journal);
310             }
311         } else {
312 ##          abort               /* it never happened */
313         }
314     }
315
316     if (status != SMS_SUCCESS && log_flags & LOG_RES)
317         com_err(whoami, status, " (Query failed)");
318     return(status);
319 }
320
321 build_qual(fmt, argc, argv, qual)
322         char *fmt;
323         int argc;
324         char *argv[];
325         char *qual;
326 {
327     register char *c;
328     register int i;
329     char *args[4];
330     char *index();
331
332     c = fmt;
333     for (i = 0; i < argc; i++) {
334         c = index(c, '%');
335         if (c++ == (char *)0) return(SMS_ARGS);
336         if (*c == 's')
337             args[i] = argv[i];
338         else if (*c == 'd')
339             *(int *)&args[i] = *(int *)argv[i]; /* sigh */
340         else
341             return(SMS_INGRES_ERR);
342     }
343
344     switch (argc) {
345     case 0:
346         strcpy(qual, fmt);
347         break;
348
349     case 1:
350         sprintf(qual, fmt, args[0]);
351         break;
352
353     case 2:
354         sprintf(qual, fmt, args[0], args[1]);
355         break;
356
357     case 3:
358         sprintf(qual, fmt, args[0], args[1], args[2]);
359         break;
360
361     case 4:
362         sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
363         break;
364     }
365     return(SMS_SUCCESS);
366 }
367
368 char *
369 build_sort(v, sort)
370     register struct validate *v;
371     char *sort;
372 {
373     register struct valobj *vo;
374     register int n;
375     char elem[16];
376
377     n = v->objcnt;
378     vo = v->valobj;
379     *sort = 0;
380
381     while (--n >= 0) {
382         if (vo->type == V_SORT) {
383             sprintf(elem, "RET_VAR%d", vo->index + 1);
384             if (*sort) strcat(sort, ", ");
385             strcat(sort, elem);
386         }
387         vo++;
388     }
389
390     return ((*sort) ? sort : 0);
391 }
392
393
394 /* Build arguement vector, verify query and arguments */
395
396 sms_verify_query(cl, q, argc, argv_ro)
397     client *cl;
398     struct query *q;
399     int argc;
400     char *argv_ro[];
401 {
402     register int argreq;
403     register int status;
404     register struct validate *v = q->validate;
405     register int i;
406     register int privileged = 0;
407
408     /* copy the arguments into a local argv that we can modify */
409     for (i = 0; i < argc; i++) {
410         if (strlen(argv_ro[i]) < ARGLEN)
411             strcpy(Argv[i], argv_ro[i]);
412         else
413             return(SMS_ARG_TOO_LONG);
414     }
415
416     /* check initial query access */
417     status = check_query_access(q, Argv, cl);
418     if (status != SMS_SUCCESS && status != SMS_PERM)
419         return(status);
420     if (status == SMS_SUCCESS)
421         privileged++;
422
423     /* check argument count */
424     argreq = q->argc;
425     if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
426     if (argc != argreq) return(SMS_ARGS);
427
428     /* validate arguments */
429     if (v && v->valobj) {
430         status = validate_fields(q, Argv, v->valobj, v->objcnt);
431         if (status != SMS_SUCCESS) return(status);
432     }
433
434     /* perform special query access check */
435     if (!privileged && v && v->acs_rtn) {
436         status = (*v->acs_rtn)(q, Argv, cl);
437         if (status != SMS_SUCCESS && status != SMS_PERM)
438             return(status);
439         if (status == SMS_SUCCESS)
440             privileged++;
441     }
442
443     return(privileged ? SMS_SUCCESS : SMS_PERM);
444 }
445
446 check_query_access(q, argv, cl)
447     struct query *q;
448     char *argv[];
449     client *cl;
450 ##{
451 ##  char *name;
452 ##  int acl_id;
453 ##  int exists;
454 ##  int rowcount;
455 ##  int errorno;
456 ##  static int def_uid;
457     int status;
458     int client_id;
459     char *client_type;
460
461     /* get query access control list */
462     name = q->shortname;
463 ##  repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
464 ##  inquire_equel (rowcount = "rowcount", errorno = "errorno")
465     if (errorno != 0) return(SMS_INGRES_ERR);
466     if (rowcount == 0) return(SMS_PERM);
467
468     /* initialize default uid */
469     if (def_uid == 0) {
470 ##      retrieve (def_uid = users.users_id) where users.login = "default"
471     }
472
473     /* check for default access */
474 ##  range of m is members
475 ##  repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
476 ##                   m.member_type = "USER" and m.#member_id = def_uid))
477     if (exists) return(SMS_SUCCESS);
478
479     /* parse client name */
480     status = get_client(cl, &client_type, &client_id);
481     if (status != SMS_SUCCESS) return(status);
482
483     /* see if client is in the list (or any of its sub-lists) */
484     exists = find_member("LIST", acl_id, client_type, client_id, 0);
485     return ((exists) ? SMS_SUCCESS : SMS_PERM);
486 ##}
487
488 get_client(cl, client_type, client_id)
489     client *cl;
490     char **client_type;
491     int *client_id;
492 ##{
493     struct krbname *krb;
494 ##  int member_id;
495 ##  char *name;
496 ##  int rowcount;
497
498     if (cl->clname == NULL)
499         return SMS_PERM;
500     
501     /* for now ignore instances */
502     krb = &cl->kname;
503
504     /* if client is from local realm, get users_id */
505     if (!strcmp(krb->realm, krb_realm)) {
506         *client_id = cl->users_id;
507         *client_type = "USER";
508         return(SMS_SUCCESS);
509     }
510
511     /* otherwise use string_id */
512     name = cl->clname;
513 ##  repeat retrieve (member_id = strings.string_id) 
514 ##      where strings.string = @name
515         
516     /* make sure we found a users or string id */
517 ##  inquire_equel (rowcount = "rowcount")
518     if (rowcount == 0) return(SMS_PERM);
519
520     *client_type = "STRING";
521     *client_id = member_id;
522     return(SMS_SUCCESS);
523 ##}
524
525 ##find_member(list_type, list_id, member_type, member_id, sq)
526     char *list_type;
527 ##  int list_id;
528 ##  char *member_type;
529 ##  int member_id;
530     struct save_queue *sq;
531 ##{
532 ##  int exists;
533 ##  int sublist;
534     int child;
535     struct save_queue *sq_create();
536
537     /* see if client is a direct member of list */
538 ##  repeat retrieve (exists = any(m.#member_id where 
539 ##                                m.#list_id = @list_id and
540 ##                                m.#member_type = @member_type and 
541 ##                                m.#member_id = @member_id))
542     if (exists) return(1);
543
544     /* are there any sub-lists? */
545 ##  repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
546 ##                   m.#member_type = "LIST"))
547     if (!exists) return(0);
548
549     /* yes; now recurse through sublists */
550
551     /* create a save queue */
552     if (sq == (struct save_queue *)0) {
553         sq = sq_create();
554         child = 0;
555     } else {
556         child = 1;
557     }
558
559     /* save all sublist ids */
560 ##  range of m is members
561 ##  retrieve (sublist = m.#member_id) 
562 ##      where m.#list_id = list_id and m.#member_type = "LIST"
563 ##  {
564          sq_save_unique_data(sq, sublist);
565 ##  }
566
567     if (child) return(0);
568
569     /* at top-level, check sub-lists for client (breadth-first search) */
570     while (sq_get_data(sq, &sublist)) {
571         exists = find_member(list_type, sublist, member_type, member_id, sq);
572         if (exists) {
573             sq_destroy(sq);
574             return(1);
575         }
576     }
577     sq_destroy(sq);
578     return(0);
579 ##}
580
581
582 do_retrieve(q, pqual, psort, action, actarg)
583     register struct query *q;
584     char *pqual;
585     char *psort;
586     int (*action)();
587     char *actarg;
588 ##{
589 ##  char *rvar;
590 ##  char *rtable;
591 ##  char *cqual;
592 ##  char *csort;
593 ##  int rowcount;
594 ##  int errorno;
595     static char **vaddrs = (char **)NULL;
596
597     if (!vaddrs) {
598         register int i;
599
600         if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
601             com_err(whoami, SMS_NO_MEM, "setting up static argv");
602             exit(1);
603         }
604         for (i = 0; i < QMAXARGS; i++) {
605             if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
606                 com_err(whoami, SMS_NO_MEM, "setting up static argv");
607                 exit(1);
608             }
609         }
610     }
611
612     if (q->rvar) {
613         rvar = q->rvar;
614         rtable = q->rtable;
615 ##      range of rvar is rtable
616     }
617
618     if (psort) {
619         csort = psort;
620         if (pqual) {
621             cqual = pqual;
622 ##          retrieve unique (param (q->tlist, vaddrs)) where cqual
623 ##                   sort by csort
624 ##          {
625                  (*action)(q->vcnt, vaddrs, actarg);
626 ##          }
627         } else {
628 ##          retrieve unique (param (q->tlist, vaddrs))
629 ##                   sort by csort
630 ##          {
631                  (*action)(q->vcnt, vaddrs, actarg);
632 ##          }
633         }
634
635     } else {
636         if (pqual) {
637             cqual = pqual;
638 ##          retrieve unique (param (q->tlist, vaddrs)) where cqual
639 ##          {
640                  (*action)(q->vcnt, vaddrs, actarg);
641 ##          }
642         } else {
643 ##          retrieve unique (param (q->tlist, vaddrs))
644 ##          {
645                  (*action)(q->vcnt, vaddrs, actarg);
646 ##          }
647         }
648     }
649
650 ##  inquire_equel (rowcount = "rowcount", errorno = "errorno")
651     if (errorno != 0) return(SMS_INGRES_ERR);
652     return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
653 ##}
654
655 do_update(q, argv, qual, action, actarg)
656     register struct query *q;
657     char *argv[];
658     char *qual;
659     int (*action)();
660     char *actarg;
661 ##{
662 ##  char *rvar;
663 ##  char *rtable;
664 ##  char *cqual;
665 ##  int errorno;
666       
667     rvar = q->rvar;
668     rtable = q->rtable;
669 ##  range of rvar is rtable
670
671     cqual = qual;
672 ##  replace rvar (param (q->tlist, argv))
673 ##  where cqual
674
675 ##  inquire_equel (errorno = "errorno")
676     if (errorno == INGRES_BAD_INT)
677         return(SMS_INTEGER);
678     else if (errorno != 0)
679         return(SMS_INGRES_ERR);
680     return(SMS_SUCCESS);
681 ##}
682
683 do_append(q, argv, pqual, action, actarg)
684     register struct query *q;
685     char *argv[];
686     char *pqual;
687     int (*action)();
688     char *actarg;
689 ##{
690 ##  char *rvar;
691 ##  char *rtable;
692 ##  char *cqual;
693 ##  int errorno;
694
695     rvar = q->rvar;
696     rtable = q->rtable;
697 ##  range of rvar is rtable
698
699     if (pqual) {
700         cqual = pqual;
701 ##      append to rtable (param (q->tlist, argv)) where cqual
702     } else {
703 ##      append to rtable (param (q->tlist, argv))
704     }
705
706 ##  inquire_equel (errorno = "errorno")
707     if (errorno == INGRES_BAD_INT)
708         return(SMS_INTEGER);
709     else if (errorno != 0)
710         return(SMS_INGRES_ERR);
711     return(SMS_SUCCESS);
712 ##}
713
714 do_delete(q, qual, action, actarg)
715     register struct query *q;
716     char *qual;
717     int (*action)();
718     char *actarg;
719 ##{
720 ##  char *rvar;
721 ##  char *rtable;
722 ##  char *cqual;
723 ##  int errorno;
724
725     rvar = q->rvar;
726     rtable = q->rtable;
727 ##  range of rvar is rtable
728
729     cqual = qual;
730 ##  delete rvar where cqual
731
732 ##  inquire_equel (errorno = "errorno")
733     if (errorno != 0) return(SMS_INGRES_ERR);
734     return(SMS_SUCCESS);
735 ##}
736
737
738 /**
739  ** set_next_object_id - set next object id in values table
740  **
741  ** Inputs: object - object name in values table and in objects
742  **         table - name of table objects are found in
743  **
744  ** - called before an APPEND operation to set the next object id to
745  **   be used for the new record to the next free value
746  **
747  **/
748
749 set_next_object_id(object, table)
750     char *object;
751     char *table;
752 ##{
753 ##  char *name, *tbl;
754 ##  int rowcount, exists, value;
755
756     name = object;
757     tbl = table;
758 ##  range of v is values
759 ##  repeat retrieve (value = v.#value) where v.#name = @name
760 ##  inquire_equel(rowcount = "rowcount")
761     if (rowcount != 1)
762         return(SMS_NO_ID);
763
764 ##  retrieve (exists = any(tbl.name where tbl.name = value))
765 ##  inquire_equel(rowcount = "rowcount")
766     if (rowcount != 1)
767         return(SMS_NO_ID);
768     while (exists) {
769         value++;
770         if (value > MAX_ID_VALUE)
771             value = MIN_ID_VALUE;
772 ##      retrieve (exists = any(tbl.name where tbl.name = value))
773     }
774
775     if (LOG_RES)
776         com_err(whoami, 0, "setting ID %s to %d", name, value);
777 ##  repeat replace v (#value = @value) where v.#name = @name
778     return(SMS_SUCCESS);
779 ##}
780
781
782 /* For now this just checks the argc's.  It should also see that there
783  * are no duplicate names.
784  */
785
786 sanity_check_queries()
787 {
788     register int i;
789     int maxv = 0, maxa = 0;
790     extern int QueryCount1, QueryCount2;
791     extern struct query Queries1[], Queries2[];
792 #define MAX(x,y) ((x) > (y) ? (x) : (y))
793
794     for (i = 0; i < QueryCount1; i++) {
795         maxv = MAX(maxv, Queries1[i].vcnt);
796         maxa = MAX(maxa, Queries1[i].argc);
797     }
798     for (i = 0; i < QueryCount2; i++) {
799         maxv = MAX(maxv, Queries2[i].vcnt);
800         maxa = MAX(maxa, Queries2[i].argc);
801     }
802     if (MAX(maxv, maxa) > QMAXARGS) {
803         com_err(whoami, 0, "A query has more args than QMAXARGS");
804         exit(1);
805     }
806 }
807
808
809 /*
810  * Local Variables:
811  * mode: c
812  * c-indent-level: 4
813  * c-continued-statement-offset: 4
814  * c-brace-offset: -4
815  * c-argdecl-indent: 4
816  * c-label-offset: -4
817  * End:
818  */
819
This page took 0.404449 seconds and 5 git commands to generate.