xref: /onnv-gate/usr/src/cmd/fps/fpsd/fpsd_util.c (revision 7346:8a16b5b71337)
16429Svs195195 /*
26429Svs195195  * CDDL HEADER START
36429Svs195195  *
46429Svs195195  * The contents of this file are subject to the terms of the
56429Svs195195  * Common Development and Distribution License (the "License").
66429Svs195195  * You may not use this file except in compliance with the License.
76429Svs195195  *
86429Svs195195  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96429Svs195195  * or http://www.opensolaris.org/os/licensing.
106429Svs195195  * See the License for the specific language governing permissions
116429Svs195195  * and limitations under the License.
126429Svs195195  *
136429Svs195195  * When distributing Covered Code, include this CDDL HEADER in each
146429Svs195195  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156429Svs195195  * If applicable, add the following below this CDDL HEADER, with the
166429Svs195195  * fields enclosed by brackets "[]" replaced with your own identifying
176429Svs195195  * information: Portions Copyright [yyyy] [name of copyright owner]
186429Svs195195  *
196429Svs195195  * CDDL HEADER END
206429Svs195195  */
216429Svs195195 
226429Svs195195 /*
236429Svs195195  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
246429Svs195195  * Use is subject to license terms.
256429Svs195195  */
266429Svs195195 
276429Svs195195 #include <stdio.h>
286429Svs195195 #include <sys/types.h>
296429Svs195195 #include <dirent.h>
306429Svs195195 #include <stdarg.h>
316429Svs195195 #include <stddef.h>
326429Svs195195 #include <stdlib.h>
336429Svs195195 #include <dlfcn.h>
346429Svs195195 #include <door.h>
356429Svs195195 #include <errno.h>
366429Svs195195 #include <fcntl.h>
376429Svs195195 #include <strings.h>
386429Svs195195 #include <unistd.h>
396429Svs195195 #include <synch.h>
406429Svs195195 #include <syslog.h>
416429Svs195195 #include <pthread.h>
426429Svs195195 #include <thread.h>
436429Svs195195 #include <signal.h>
446429Svs195195 #include <limits.h>
456429Svs195195 #include <locale.h>
466429Svs195195 #include <sys/stat.h>
476429Svs195195 #include <sys/systeminfo.h>
486429Svs195195 #include <sys/wait.h>
496429Svs195195 #include <sys/processor.h>
506429Svs195195 #include <ctype.h>
516429Svs195195 #include <poll.h>
526429Svs195195 #include <sys/wait.h>
536429Svs195195 #include <sys/swap.h>
546429Svs195195 
556429Svs195195 #include <fpsapi.h>
566429Svs195195 #include "messages.h"
576429Svs195195 #include "fpsd.h"
586429Svs195195 
596429Svs195195 /* Exported Functions */
606429Svs195195 void fps_door_handler(void *cookie, char *argp, size_t asize,
616429Svs195195 	door_desc_t  *dp, uint_t  n_desc);
626429Svs195195 
63*7186Skk158166 /* Used by get_free_swap() */
646429Svs195195 static uint64_t
ctok(int clicks)656429Svs195195 ctok(int clicks)
666429Svs195195 {
676429Svs195195 	static int factor = -1;
686429Svs195195 
696429Svs195195 	if (factor == -1) factor = ((int)sysconf(_SC_PAGESIZE)) >> 10;
706429Svs195195 	return (clicks*factor);
716429Svs195195 }
726429Svs195195 
736429Svs195195 /* return the available free swap space in unit of MB */
746429Svs195195 uint64_t
get_free_swap(void)756429Svs195195 get_free_swap(void)
766429Svs195195 {
776429Svs195195 	struct anoninfo ai;
786429Svs195195 	unsigned freemem;
796429Svs195195 
806429Svs195195 	if (swapctl(SC_AINFO, &ai) != -1) {
816429Svs195195 		/* in the unit of KB */
826429Svs195195 		freemem = (int)(ctok(ai.ani_max) - ctok(ai.ani_resv));
836429Svs195195 	}
846429Svs195195 	else
856429Svs195195 		freemem = 0;
866429Svs195195 
876429Svs195195 	return (freemem/1024);
886429Svs195195 }
896429Svs195195 
906429Svs195195 /*
916429Svs195195  *  Wait for n secs. Don't use sleep due to signal behaviours.
926429Svs195195  *  Also be aware of poll getting interrupted.
936429Svs195195  */
946429Svs195195 
956429Svs195195 void
fps_wait_secs(int secs)966429Svs195195 fps_wait_secs(int secs)
976429Svs195195 {
986429Svs195195 	time_t cur = time(NULL);
996429Svs195195 
1006429Svs195195 	if (secs <= 0)
1016429Svs195195 		return;
1026429Svs195195 
1036429Svs195195 	do {
1046429Svs195195 		if (poll(NULL, 0, secs*1000) == 0)
1056429Svs195195 			break;
1066429Svs195195 		secs -= (int)(time(NULL) - cur);
1076429Svs195195 		cur   = time(NULL);
1086429Svs195195 	} while (secs > 0);
1096429Svs195195 }
1106429Svs195195 
1116429Svs195195 /*ARGSUSED*/
1126429Svs195195 void
fps_door_handler(void * cookie,char * argp,size_t asize,door_desc_t * dp,uint_t n_desc)1136429Svs195195 fps_door_handler(void *cookie, char *argp, size_t asize,
1146429Svs195195 	door_desc_t  *dp, uint_t  n_desc)
1156429Svs195195 {
1166429Svs195195 	fps_event_t	*evtp = NULL;
1176429Svs195195 	fps_event_reply_t	reply;
1186429Svs195195 
1196429Svs195195 	reply.result = -1;  /* -1 failure. 0 success */
1206429Svs195195 
1216429Svs195195 	if (argp == NULL)
1226429Svs195195 		(void) door_return((char *)&reply, sizeof (reply), NULL, 0);
1236429Svs195195 
1246429Svs195195 	/*LINTED*/
1256429Svs195195 	evtp  = (fps_event_t *)argp;
1266429Svs195195 
1276429Svs195195 	if (cookie != FPS_DOOR_COOKIE)
1286429Svs195195 		(void) door_return((char *)&reply, sizeof (reply), NULL, 0);
1296429Svs195195 
1306429Svs195195 	fpsd_message(FPSD_NO_EXIT, FPS_INFO,
1316429Svs195195 	    DOOR_HNDLR_MSG,
1326429Svs195195 	    evtp->version, evtp->type, evtp->length);
1336429Svs195195 
1346429Svs195195 	reply.result = 0;
1356429Svs195195 	(void) door_return((char *)&reply, sizeof (reply), NULL, 0);
1366429Svs195195 
1376429Svs195195 }
138