PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/cmd/rcap/common/utils.c

https://bitbucket.org/a3217055/illumos-2
C | 333 lines | 238 code | 55 blank | 40 comment | 44 complexity | df0b8c5959c767ea2f47c46691c0f15b MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0, BSD-3-Clause, LGPL-2.0, 0BSD, BSD-2-Clause, BSD-3-Clause-No-Nuclear-License-2014, AGPL-1.0, GPL-2.0
  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. * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  23. */
  24. #include <sys/param.h>
  25. #include <libintl.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <strings.h>
  30. #include <syslog.h>
  31. #include <unistd.h>
  32. #include <errno.h>
  33. #include "utils.h"
  34. static char ERRNO_FMT[] = ": %s";
  35. static char *pname = NULL;
  36. static rcm_level_t message_priority = RCM_WARN;
  37. static rcm_dst_t message_dst = RCD_STD;
  38. static void dmesg(int level, char *msg);
  39. /*PRINTFLIKE2*/
  40. void
  41. dprintfe(int level, char *format, ...)
  42. {
  43. va_list alist;
  44. va_start(alist, format);
  45. vdprintfe(level, format, alist);
  46. va_end(alist);
  47. }
  48. /*PRINTFLIKE2*/
  49. void
  50. vdprintfe(int level, const char *format, va_list alist)
  51. {
  52. char buf[LINELEN];
  53. char *c;
  54. int err = errno;
  55. *buf = 0;
  56. if ((strlen(buf) + 1) < LINELEN)
  57. (void) vsnprintf(buf + strlen(buf), LINELEN - 1 - strlen(buf),
  58. format, alist);
  59. if ((c = strchr(buf, '\n')) == NULL) {
  60. if ((strlen(buf) + 1) < LINELEN)
  61. (void) snprintf(buf + strlen(buf), LINELEN - 1 -
  62. strlen(buf), gettext(ERRNO_FMT), strerror(err));
  63. } else
  64. *c = 0;
  65. dmesg(level, buf);
  66. }
  67. #ifdef DEBUG_MSG
  68. /*PRINTFLIKE1*/
  69. void
  70. debug(char *format, ...)
  71. {
  72. va_list alist;
  73. if (get_message_priority() < RCM_DEBUG)
  74. return;
  75. va_start(alist, format);
  76. vdprintfe(RCM_DEBUG, format, alist);
  77. va_end(alist);
  78. }
  79. /*PRINTFLIKE1*/
  80. void
  81. debug_high(char *format, ...)
  82. {
  83. va_list alist;
  84. if (get_message_priority() < RCM_DEBUG_HIGH)
  85. return;
  86. va_start(alist, format);
  87. vdprintfe(RCM_DEBUG_HIGH, format, alist);
  88. va_end(alist);
  89. }
  90. #endif /* DEBUG_MSG */
  91. /*PRINTFLIKE1*/
  92. void
  93. warn(const char *format, ...)
  94. {
  95. va_list alist;
  96. if (get_message_priority() < RCM_WARN)
  97. return;
  98. va_start(alist, format);
  99. vdprintfe(RCM_WARN, format, alist);
  100. va_end(alist);
  101. }
  102. /*PRINTFLIKE1*/
  103. void
  104. die(char *format, ...)
  105. {
  106. va_list alist;
  107. if (get_message_priority() < RCM_ERR)
  108. return;
  109. va_start(alist, format);
  110. vdprintfe(RCM_ERR, format, alist);
  111. va_end(alist);
  112. exit(E_ERROR);
  113. }
  114. /*PRINTFLIKE1*/
  115. void
  116. info(char *format, ...)
  117. {
  118. va_list alist;
  119. if (get_message_priority() < RCM_INFO)
  120. return;
  121. va_start(alist, format);
  122. vdprintfe(RCM_INFO, format, alist);
  123. va_end(alist);
  124. }
  125. char *
  126. setpname(char *arg0)
  127. {
  128. char *p = strrchr(arg0, '/');
  129. if (p == NULL)
  130. p = arg0;
  131. else
  132. p++;
  133. pname = p;
  134. return (pname);
  135. }
  136. /*
  137. * Output a message to the controlling tty or log, depending on which is
  138. * configured. The message should contain no newlines.
  139. */
  140. static void
  141. dmesg(int level, char *msg)
  142. {
  143. if (message_priority >= level) {
  144. FILE *fp;
  145. int syslog_severity = -1;
  146. switch (message_dst) {
  147. case RCD_STD:
  148. fp = level >= RCM_DEBUG ? stderr : stdout;
  149. if (pname != NULL) {
  150. (void) fputs(pname, fp);
  151. (void) fputs(": ", fp);
  152. }
  153. (void) fputs(msg, fp);
  154. (void) fputc('\n', fp);
  155. (void) fflush(fp);
  156. break;
  157. case RCD_SYSLOG:
  158. switch (level) {
  159. case RCM_ERR:
  160. syslog_severity = LOG_ERR;
  161. break;
  162. case RCM_WARN:
  163. syslog_severity = LOG_WARNING;
  164. break;
  165. case RCM_INFO:
  166. syslog_severity = LOG_INFO;
  167. break;
  168. case RCM_DEBUG:
  169. syslog_severity = LOG_DEBUG;
  170. break;
  171. }
  172. if (syslog_severity >= 0)
  173. (void) syslog(syslog_severity, "%s", msg);
  174. break;
  175. }
  176. }
  177. }
  178. rcm_level_t
  179. get_message_priority(void)
  180. {
  181. return (message_priority);
  182. }
  183. rcm_level_t
  184. set_message_priority(rcm_level_t new_priority)
  185. {
  186. rcm_level_t old_priority = message_priority;
  187. message_priority = new_priority;
  188. return (old_priority);
  189. }
  190. rcm_dst_t
  191. set_message_destination(rcm_dst_t new_dst)
  192. {
  193. rcm_dst_t old_dst = message_dst;
  194. if ((message_dst = new_dst) == RCD_SYSLOG)
  195. openlog(pname, LOG_ODELAY | LOG_PID, LOG_DAEMON);
  196. return (old_dst);
  197. }
  198. void
  199. hrt2ts(hrtime_t hrt, timestruc_t *tsp)
  200. {
  201. tsp->tv_sec = hrt / NANOSEC;
  202. tsp->tv_nsec = hrt % NANOSEC;
  203. }
  204. int
  205. xatoi(char *p)
  206. {
  207. int i;
  208. char *q;
  209. errno = 0;
  210. i = (int)strtol(p, &q, 10);
  211. if (errno != 0 || q == p || i < 0 || *q != '\0') {
  212. warn(gettext("illegal argument -- %s\n"), p);
  213. return (-1);
  214. } else {
  215. return (i);
  216. }
  217. }
  218. /*
  219. * get_running_zones() calls zone_list(2) to find out how many zones are
  220. * running. It then calls zone_list(2) again to fetch the list of running
  221. * zones (stored in *zents).
  222. */
  223. int
  224. get_running_zones(uint_t *nzents, zone_entry_t **zents)
  225. {
  226. zoneid_t *zids;
  227. uint_t nzents_saved;
  228. int i;
  229. zone_entry_t *zentp;
  230. zone_state_t zstate;
  231. *zents = NULL;
  232. if (zone_list(NULL, nzents) != 0) {
  233. warn(gettext("could not get zoneid list\n"));
  234. return (E_ERROR);
  235. }
  236. again:
  237. if (*nzents == 0)
  238. return (E_SUCCESS);
  239. if ((zids = (zoneid_t *)calloc(*nzents, sizeof (zoneid_t))) == NULL) {
  240. warn(gettext("out of memory: zones will not be capped\n"));
  241. return (E_ERROR);
  242. }
  243. nzents_saved = *nzents;
  244. if (zone_list(zids, nzents) != 0) {
  245. warn(gettext("could not get zone list\n"));
  246. free(zids);
  247. return (E_ERROR);
  248. }
  249. if (*nzents != nzents_saved) {
  250. /* list changed, try again */
  251. free(zids);
  252. goto again;
  253. }
  254. *zents = calloc(*nzents, sizeof (zone_entry_t));
  255. if (*zents == NULL) {
  256. warn(gettext("out of memory: zones will not be capped\n"));
  257. free(zids);
  258. return (E_ERROR);
  259. }
  260. zentp = *zents;
  261. for (i = 0; i < *nzents; i++) {
  262. char name[ZONENAME_MAX];
  263. if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
  264. warn(gettext("could not get name for "
  265. "zoneid %d\n"), zids[i]);
  266. continue;
  267. }
  268. (void) strlcpy(zentp->zname, name, sizeof (zentp->zname));
  269. zentp->zid = zids[i];
  270. if (zone_get_state(name, &zstate) != Z_OK ||
  271. zstate != ZONE_STATE_RUNNING)
  272. continue;
  273. zentp++;
  274. }
  275. *nzents = zentp - *zents;
  276. free(zids);
  277. return (E_SUCCESS);
  278. }