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, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  28 /*        All Rights Reserved   */
  29 
  30 /*
  31  * University Copyright- Copyright (c) 1982, 1986, 1988
  32  * The Regents of the University of California
  33  * All Rights Reserved
  34  *
  35  * University Acknowledgment- Portions of this document are derived from
  36  * software developed by the University of California, Berkeley, and its
  37  * contributors.
  38  */
  39 
  40 /*
  41  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  42  * Copyright (c) 2012 by Delphix. All rights reserved.
  43  */
  44 
  45 #ifndef _SM_STATD_H
  46 #define _SM_STATD_H
  47 
  48 #ifdef __cplusplus
  49 extern "C" {
  50 #endif
  51 
  52 /* Limit defines */
  53 #define SM_DIRECTORY_MODE 00755
  54 #define MAX_HASHSIZE 50
  55 #define SM_RPC_TIMEOUT  15
  56 #define PERCENT_MINJOIN 10
  57 #define MAX_FDS 256
  58 #define MAX_THR 25
  59 #define INC_DELAYTIME   30
  60 #define MAX_DELAYTIME   300
  61 #define SM_CLTS_TIMEOUT 15
  62 /* max strlen of /statmon/state, /statmon/sm.bak, /statmon/sm */
  63 #define SM_MAXPATHLEN   17
  64 /* Increment size for realloc of array host_name */
  65 #define HOST_NAME_INCR  5
  66 
  67 /* supported address family names in /var/statmon symlinks */
  68 #define SM_ADDR_IPV4    "ipv4"
  69 #define SM_ADDR_IPV6    "ipv6"
  70 
  71 /* Supported for readdir_r() */
  72 #define MAXDIRENT       (sizeof (struct dirent) +  _POSIX_PATH_MAX + 1)
  73 
  74 /* Structure entry for monitor table (mon_table) */
  75 struct mon_entry {
  76         mon id;                         /* mon information: mon_name, my_id */
  77         struct mon_entry *prev;         /* Prev ptr to prev entry in hash */
  78         struct mon_entry *nxt;          /* Next ptr to next entry in hash */
  79 };
  80 typedef struct mon_entry mon_entry;
  81 
  82 /* Structure entry for record (rec_table) and recovery (recov_q) tables */
  83 struct name_entry {
  84         char *name;                     /* name of host */
  85         int count;                      /* count of entries */
  86         struct name_entry *prev;        /* Prev ptr to prev entry in hash */
  87         struct name_entry *nxt;         /* Next ptr to next entry in hash */
  88 };
  89 typedef struct name_entry name_entry;
  90 
  91 /* Structure for passing arguments into thread send_notice */
  92 typedef struct moninfo {
  93         mon id;                         /* Monitor information */
  94         int state;                      /* Current state */
  95 } moninfo_t;
  96 
  97 /* Structure entry for hash tables */
  98 typedef struct sm_hash {
  99         union {
 100                 struct mon_entry *mon_hdptr;    /* Head ptr for mon_table */
 101                 name_entry *rec_hdptr;          /* Head ptr for rec_table */
 102                 name_entry *recov_hdptr;        /* Head ptr for recov_q */
 103         } smhd_t;
 104         mutex_t lock;                   /* Lock to protect each list head */
 105 } sm_hash_t;
 106 
 107 #define sm_monhdp       smhd_t.mon_hdptr
 108 #define sm_rechdp       smhd_t.rec_hdptr
 109 #define sm_recovhdp     smhd_t.recov_hdptr
 110 
 111 /* Structure entry for address list in name-to-address entry */
 112 typedef struct addr_entry {
 113         struct addr_entry *next;
 114         struct netobj ah;
 115         sa_family_t family;
 116 } addr_entry_t;
 117 
 118 /* Structure entry for name-to-address translation table */
 119 typedef struct name_addr_entry {
 120         struct name_addr_entry *next;
 121         char *name;
 122         struct addr_entry *addresses;
 123 } name_addr_entry_t;
 124 
 125 /* Hash tables for each of the in-cache information */
 126 extern sm_hash_t        mon_table[MAX_HASHSIZE];
 127 
 128 /* Global variables */
 129 extern mutex_t crash_lock;      /* lock for die and crash variables */
 130 extern int die;                 /* Flag to indicate that an SM_CRASH */
 131                                 /* request came in & to stop threads cleanly */
 132 extern int in_crash;            /* Flag to single thread sm_crash requests. */
 133 extern int regfiles_only;       /* Flag to indicate symlink use in statmon */
 134 extern cond_t crash_finish;     /* Condition to wait until crash is finished */
 135 extern mutex_t sm_trylock;      /* Lock to single thread sm_try */
 136 /*
 137  * The only established lock precedence here is:
 138  *
 139  *      thr_rwlock > name_addrlock
 140  */
 141 extern mutex_t name_addrlock;   /* Locks all entries of name-to-addr table */
 142 extern rwlock_t thr_rwlock;     /* Reader/writer lock for requests coming in */
 143 extern cond_t retrywait;        /* Condition to wait before starting retry */
 144 
 145 extern char STATE[MAXPATHLEN], CURRENT[MAXPATHLEN];
 146 extern char BACKUP[MAXPATHLEN];
 147 extern int LOCAL_STATE;
 148 
 149 /*
 150  * Hash functions for monitor and record hash tables.
 151  * Functions are hashed based on first 2 letters and last 2 letters of name.
 152  * If only 1 letter in name, then, hash only on 1 letter.
 153  */
 154 #define SMHASH(name, key) { \
 155         int l; \
 156         key = *name; \
 157         if ((l = strlen(name)) != 1) \
 158                 key |= ((*(name+(l-1)) << 24) | (*(name+1) << 16) | \
 159                         (*(name+(l-2)) << 8)); \
 160         key = key % MAX_HASHSIZE; \
 161 }
 162 
 163 extern int debug;               /* Prints out debug information if set. */
 164 
 165 extern char hostname[MAXHOSTNAMELEN];
 166 
 167 /*
 168  * These variables will be used to store all the
 169  * alias names for the host, as well as the -a
 170  * command line hostnames.
 171  */
 172 extern char **host_name; /* store -a opts */
 173 extern int      host_name_count;
 174 extern int  addrix; /* # of -a entries */
 175 
 176 /*
 177  * The following 2 variables are meaningful
 178  * only under a HA configuration.
 179  */
 180 extern char **path_name; /* store -p opts */
 181 extern int  pathix; /* # of -p entries */
 182 
 183 /* Function prototypes used in program */
 184 extern int      create_file(char *name);
 185 extern void     delete_file(char *name);
 186 extern void     record_name(char *name, int op);
 187 extern void     sm_crash(void);
 188 extern void     statd_init();
 189 extern void     merge_hosts(void);
 190 extern void     merge_ips(void);
 191 extern CLIENT   *create_client(char *, int, int, char *, struct timeval *);
 192 extern char     *xmalloc(unsigned);
 193 
 194 /*
 195  * RPC service functions, slightly different here than the
 196  * generated ones in sm_inter.h
 197  */
 198 extern void     nsmaddrproc1_reg(reg1args *, reg1res *);
 199 extern void     sm_stat_svc(sm_name *namep, sm_stat_res *resp);
 200 extern void     sm_mon_svc(mon *monp, sm_stat_res *resp);
 201 extern void     sm_unmon_svc(mon_id *monidp, sm_stat *resp);
 202 extern void     sm_unmon_all_svc(my_id *myidp, sm_stat *resp);
 203 extern void     sm_simu_crash_svc(void *myidp);
 204 extern void     sm_notify_svc(stat_chge *ntfp);
 205 
 206 extern void     sm_inithash();
 207 extern void     copydir_from_to(char *from_dir, char *to_dir);
 208 extern int      str_cmp_unqual_hostname(char *, char *);
 209 extern void     record_addr(char *name, sa_family_t family, struct netobj *ah);
 210 extern int      is_symlink(char *file);
 211 extern int      create_symlink(char *todir, char *rname, char *lname);
 212 extern int      str_cmp_address_specifier(char *specifier1, char *specifier2);
 213 
 214 #ifdef __cplusplus
 215 }
 216 #endif
 217 
 218 #endif /* _SM_STATD_H */