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