xref: /onnv-gate/usr/src/cmd/fps/fpsd/fpsd_util.c (revision 7186:e728311aafb0)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32 #include <stdarg.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <dlfcn.h>
36 #include <door.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <strings.h>
40 #include <unistd.h>
41 #include <synch.h>
42 #include <syslog.h>
43 #include <pthread.h>
44 #include <thread.h>
45 #include <signal.h>
46 #include <limits.h>
47 #include <locale.h>
48 #include <sys/stat.h>
49 #include <sys/systeminfo.h>
50 #include <sys/wait.h>
51 #include <sys/processor.h>
52 #include <ctype.h>
53 #include <poll.h>
54 #include <sys/wait.h>
55 #include <sys/swap.h>
56 
57 #include <fpsapi.h>
58 #include "messages.h"
59 #include "fpsd.h"
60 
61 /* Exported Functions */
62 void fps_door_handler(void *cookie, char *argp, size_t asize,
63 	door_desc_t  *dp, uint_t  n_desc);
64 
65 /* Used by get_free_swap() */
66 static uint64_t
67 ctok(int clicks)
68 {
69 	static int factor = -1;
70 
71 	if (factor == -1) factor = ((int)sysconf(_SC_PAGESIZE)) >> 10;
72 	return (clicks*factor);
73 }
74 
75 /* return the available free swap space in unit of MB */
76 uint64_t
77 get_free_swap(void)
78 {
79 	struct anoninfo ai;
80 	unsigned freemem;
81 
82 	if (swapctl(SC_AINFO, &ai) != -1) {
83 		/* in the unit of KB */
84 		freemem = (int)(ctok(ai.ani_max) - ctok(ai.ani_resv));
85 	}
86 	else
87 		freemem = 0;
88 
89 	return (freemem/1024);
90 }
91 
92 /*
93  *  Wait for n secs. Don't use sleep due to signal behaviours.
94  *  Also be aware of poll getting interrupted.
95  */
96 
97 void
98 fps_wait_secs(int secs)
99 {
100 	time_t cur = time(NULL);
101 
102 	if (secs <= 0)
103 		return;
104 
105 	do {
106 		if (poll(NULL, 0, secs*1000) == 0)
107 			break;
108 		secs -= (int)(time(NULL) - cur);
109 		cur   = time(NULL);
110 	} while (secs > 0);
111 }
112 
113 /*ARGSUSED*/
114 void
115 fps_door_handler(void *cookie, char *argp, size_t asize,
116 	door_desc_t  *dp, uint_t  n_desc)
117 {
118 	fps_event_t	*evtp = NULL;
119 	fps_event_reply_t	reply;
120 
121 	reply.result = -1;  /* -1 failure. 0 success */
122 
123 	if (argp == NULL)
124 		(void) door_return((char *)&reply, sizeof (reply), NULL, 0);
125 
126 	/*LINTED*/
127 	evtp  = (fps_event_t *)argp;
128 
129 	if (cookie != FPS_DOOR_COOKIE)
130 		(void) door_return((char *)&reply, sizeof (reply), NULL, 0);
131 
132 	fpsd_message(FPSD_NO_EXIT, FPS_INFO,
133 	    DOOR_HNDLR_MSG,
134 	    evtp->version, evtp->type, evtp->length);
135 
136 	reply.result = 0;
137 	(void) door_return((char *)&reply, sizeof (reply), NULL, 0);
138 
139 }
140