1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #ifndef _STATE_H
  28 #define _STATE_H
  29 
  30 /*
  31  * Describes the state of the daemon
  32  */
  33 
  34 #ifdef __cplusplus
  35 extern "C" {
  36 #endif
  37 
  38 #include <sys/types.h>
  39 #include <sys/param.h>
  40 #include <synch.h>
  41 
  42 #define METHNAMELENGTH  20
  43 #define METHNAMECOUNT   100
  44 #define MAXCHILD        10
  45 #define SVC_ALL_STOP    (~0U)
  46 #define SVC_NO_STOP     (~1U)
  47 
  48 /*
  49  * This item is an activity.
  50  * It performs the method passed with the data (a string) as the parameter.
  51  * This allows you to invoke functions in the service to perform some of
  52  * the test operations.
  53  */
  54 typedef struct activity {
  55         int             (*method)(char *);
  56         char            *data;
  57         struct activity *next;
  58 } activity_t;
  59 
  60 /* chain of report functions */
  61 typedef struct reportfn {
  62         char            *name;                  /* saved for output */
  63         void            (*method)(void);        /* method to call */
  64         struct reportfn *next;                  /* next method to call */
  65 } reportfn_t;
  66 
  67 /* Volatile service state ... not persisted into the mmaped filed */
  68 typedef struct servicestate_v {
  69         char            savefile[MAXPATHLEN];   /* file to save state */
  70         char            method[MAXPATHLEN]; /* methods to perform */
  71         char            servicename[MAXPATHLEN];        /* service name */
  72         char            serviceinst[MAXPATHLEN];        /* service instance */
  73         int             numchildren;    /* number of child processes */
  74         activity_t      *activity; /* Activity to do */
  75         char            activity_string[MAXPATHLEN];
  76                                         /* a new reaction for the service */
  77         reportfn_t      *functions;  /* a set of functions to report state */
  78 } servicestate_v_t;
  79 
  80 typedef struct servicestate_p {
  81         uint32_t        version;        /* version number */
  82         mutex_t         lock;
  83         uint32_t        service_count;  /* Count of service sub-processes */
  84         pid_t           service_pgid;   /* Service process group */
  85         pid_t           service_pid[MAXCHILD];  /* pids of the service */
  86         uint32_t        returncode;     /* value of the returncode from stop */
  87         uint32_t        stopmeflag;     /* flag to decide if it terminates */
  88         uint32_t        dosegv;         /* Flag to trigger SEGV */
  89         uint32_t        dobuserr;       /* Flag to trigger BUSERR */
  90         uint32_t        stopval;        /* stop with certain value */
  91         uint32_t        headcalled;     /* count of methods called */
  92         uint32_t        childtofork;    /* child# to cause fork */
  93         uint32_t        segvchild;      /* child# to SEGV after fork */
  94         uint32_t        lockdown;       /* child# to wire down memory */
  95         uint32_t        forkexec;       /* child# to issue fork-exec */
  96         char            commandline[MAXPATHLEN];
  97                                         /* command line for fork-exec */
  98         char            methodscalled[METHNAMECOUNT][METHNAMELENGTH];
  99                                         /* The methods that have been invoked */
 100         char            servicename[MAXPATHLEN];        /* service name */
 101         char            serviceinst[MAXPATHLEN];        /* service instance */
 102 } servicestate_p_t;
 103 
 104 #define PERSIST_VERSION ((1<<4) | 6)
 105 
 106 /* saved and unsaved service state ... saved state is guaranteed */
 107 extern servicestate_p_t *saved;
 108 extern servicestate_v_t *unsaved;
 109 
 110 /* functions */
 111 int read_switches(int *argc, char **argv);
 112 void robust_lock(mutex_t *mutex);
 113 
 114 activity_t *find_activity(activity_t *list, const char *methname);
 115 activity_t *create_activitylist(void);
 116 void destroy_activitylist(activity_t *list);
 117 activity_t *add_activity(activity_t *list, const char *methname,
 118     int(*activity)(struct activity *), char *value);
 119 char *get_reportfunctions(servicestate_v_t *state);
 120 void invoke_reportfns(servicestate_v_t *state);
 121 void record_invocation(servicestate_p_t *state, char *method);
 122 char *last_invocation(servicestate_p_t *state);
 123 int count_invocation(servicestate_p_t *state, char *method);
 124 int invocation_offset(servicestate_p_t *state, char *method);
 125 void dump_savedstate(servicestate_p_t *state);
 126 void free_activity(activity_t *activity);
 127 activity_t *parse_activity(char *text);
 128 
 129 #ifdef __cplusplus
 130 }
 131 #endif
 132 
 133 #endif /* _STATE_H */