FreeBSD/usr.sbin/kgmon/kgmon.c
/*
 * Check that profiling is enabled and open any necessary files.
 */
Check that profiling is enabled and open any necessary files.
Check that profiling is enabled and open any necessary files .
int
openfiles(const char *systemname, char *kmemf, struct kvmvars *kvp)
{
	size_t size;
	int mib[3], state, openmode;
	char errbuf[_POSIX2_LINE_MAX];
	if (!kflag) {
		mib[0] = CTL_KERN;
		mib[1] = KERN_PROF;
		mib[2] = GPROF_STATE;
		size = sizeof state;
		if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
			errx(20, "profiling not defined in kernel");
		if (!(Bflag || bflag || hflag || rflag ||
		    (pflag &&
		     (state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
			return (O_RDONLY);
		(void)seteuid(0);
		if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
			return (O_RDWR);
		(void)seteuid(getuid());
		kern_readonly(state);
		return (O_RDONLY);
	}
	openmode = (Bflag || bflag || hflag || pflag || rflag)
		   ? O_RDWR : O_RDONLY;
	kvp->kd = kvm_openfiles(systemname, kmemf, NULL, openmode, errbuf);
	if (kvp->kd == NULL) {
		if (openmode == O_RDWR) {
			openmode = O_RDONLY;
			kvp->kd = kvm_openfiles(systemname, kmemf, NULL, O_RDONLY,
			    errbuf);
		}
		if (kvp->kd == NULL)
			errx(2, "kvm_openfiles: %s", errbuf);
		kern_readonly(GMON_PROF_ON);
	}
	if (kvm_nlist(kvp->kd, nl) < 0)
		errx(3, "%s: no namelist", systemname);
	if (!nl[N_GMONPARAM].n_value)
		errx(20, "profiling not defined in kernel");
	return (openmode);
}
int openfiles ( const char * systemname , char * kmemf , struct kvmvars * kvp ) { size_t size ; int mib [ 3 ] , state , openmode ; char errbuf [ _POSIX2_LINE_MAX ] ; if ( ! kflag ) { mib [ 0 ] = CTL_KERN ; mib [ 1 ] = KERN_PROF ; mib [ 2 ] = GPROF_STATE ; size = sizeof state ; if ( sysctl ( mib , 3 , & state , & size , NULL , 0 ) < 0 ) errx ( 20 , " " ) ; if ( ! ( Bflag || bflag || hflag || rflag || ( pflag && ( state == GMON_PROF_HIRES || state == GMON_PROF_ON ) ) ) ) return ( O_RDONLY ) ; ( void ) seteuid ( 0 ) ; if ( sysctl ( mib , 3 , NULL , NULL , & state , size ) >= 0 ) return ( O_RDWR ) ; ( void ) seteuid ( getuid ( ) ) ; kern_readonly ( state ) ; return ( O_RDONLY ) ; } openmode = ( Bflag || bflag || hflag || pflag || rflag ) ? O_RDWR : O_RDONLY ; kvp -> kd = kvm_openfiles ( systemname , kmemf , NULL , openmode , errbuf ) ; if ( kvp -> kd == NULL ) { if ( openmode == O_RDWR ) { openmode = O_RDONLY ; kvp -> kd = kvm_openfiles ( systemname , kmemf , NULL , O_RDONLY , errbuf ) ; } if ( kvp -> kd == NULL ) errx ( 2 , " " , errbuf ) ; kern_readonly ( GMON_PROF_ON ) ; } if ( kvm_nlist ( kvp -> kd , nl ) < 0 ) errx ( 3 , " " , systemname ) ; if ( ! nl [ N_GMONPARAM ] . n_value ) errx ( 20 , " " ) ; return ( openmode ) ; }
Check that profiling is enabled and open any necessary files.
Check that profiling is enabled and open any necessary files .