1331722Seadler/*
257257Sdufault * Copyright (c) 1996 - 1999
357257Sdufault *	HD Associates, Inc.  All rights reserved.
457257Sdufault *
557257Sdufault * Redistribution and use in source and binary forms, with or without
657257Sdufault * modification, are permitted provided that the following conditions
757257Sdufault * are met:
857257Sdufault * 1. Redistributions of source code must retain the above copyright
957257Sdufault *    notice, this list of conditions and the following disclaimer.
1057257Sdufault * 2. Redistributions in binary form must reproduce the above copyright
1157257Sdufault *    notice, this list of conditions and the following disclaimer in the
1257257Sdufault *    documentation and/or other materials provided with the distribution.
1357257Sdufault * 3. All advertising materials mentioning features or use of this software
1457257Sdufault *    must display the following acknowledgement:
1557257Sdufault *	This product includes software developed by HD Associates, Inc
1657257Sdufault * 4. Neither the name of the author nor the names of any co-contributors
1757257Sdufault *    may be used to endorse or promote products derived from this software
1857257Sdufault *    without specific prior written permission.
1957257Sdufault *
2057257Sdufault * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
2157257Sdufault * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2257257Sdufault * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2357257Sdufault * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
2457257Sdufault * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2557257Sdufault * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2657257Sdufault * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2757257Sdufault * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2857257Sdufault * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2957257Sdufault * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3057257Sdufault * SUCH DAMAGE.
3157257Sdufault *
3257257Sdufault * $FreeBSD$
3357257Sdufault */
3457257Sdufault#include <unistd.h>
3557257Sdufault#include <stdio.h>
3657257Sdufault#include <errno.h>
3757257Sdufault#include <sys/mman.h>
3857257Sdufault
3957257Sdufault#include "prutil.h"
4057257Sdufault
4157257Sdufaultint memlock(int argc, char *argv[])
4257257Sdufault{
4357257Sdufault	int e = 0;
4457257Sdufault
4557257Sdufault	/* Is memory locking configured?
4657257Sdufault	 */
4757257Sdufault	errno = 0;
4857257Sdufault	if (sysconf(_SC_MEMLOCK) == -1) {
4957257Sdufault		if (errno != 0) {
5057257Sdufault			/* This isn't valid - may be a standard violation
5157257Sdufault			 */
5257257Sdufault			quit("(should not happen) sysconf(_SC_MEMLOCK)");
5357257Sdufault		}
5457257Sdufault		else {
5557257Sdufault			fprintf(stderr,
5657257Sdufault			"Memory locking is not supported in this environment.\n");
5757257Sdufault			e = -1;
5857257Sdufault		}
5957257Sdufault	}
6057257Sdufault
6157257Sdufault	/* Lock yourself in memory:
6257257Sdufault	 */
6357257Sdufault	if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
6457257Sdufault		perror("mlockall(MCL_CURRENT | MCL_FUTURE)");
6557257Sdufault		e = errno;
6657257Sdufault	}
6757257Sdufault	else if (munlockall() == -1) {
6857257Sdufault		perror("munlockall");
6957257Sdufault		e = errno;
7057257Sdufault	}
7157257Sdufault
7257257Sdufault	return e;
7357257Sdufault}
7457257Sdufault
7557257Sdufault#ifdef NO_MEMLOCK
7657257Sdufaultint mlockall(int flags)
7757257Sdufault{
7857257Sdufault	return EOPNOTSUPP;
7957257Sdufault}
8057257Sdufault
8157257Sdufaultint munlockall(void)
8257257Sdufault{
8357257Sdufault	return EOPNOTSUPP;
8457257Sdufault}
8557257Sdufault
8657257Sdufault
8757257Sdufault#endif
8857257Sdufault
8957257Sdufault#ifdef STANDALONE_TESTS
9057257Sdufaultint main(int argc, char *argv[]) { return memlock(argc, argv); }
9157257Sdufault#endif
92