/*
 * Copyright (c) 2005 Sendmail, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include "sm/generic.h"
SM_RCSID("@(#)$Id: sm_prtrlimits.c,v 1.4 2006/05/02 17:13:40 ca Exp $")

#include "sm/assert.h"
#include "sm/error.h"
#include "sm/io.h"
#include "sm/str.h"
#include "sm/resource.h"
#include "sm/misc.h"

#if HAVE_GETRLIMIT
/*
**  SM_PRTRLIMIT -- print rlimit to str
**
**	Parameters:
**		name -- name of limit
**		str -- string for output
**
**	Returns:
**		usual sm_error code
*/

static sm_ret_T
sm_prtrlimit(struct rlimit rl, const char *name, sm_str_P str)
{
	sm_ret_T ret;

	SM_REQUIRE(name != NULL);
	SM_REQUIRE(str != NULL);
	ret = sm_str_scat(str, name);
	if (sm_is_err(ret))
		return ret;
	if (RLIM_INFINITY == rl.rlim_cur)
	{
		ret = sm_str_scat(str, "unlimited/");
		if (sm_is_err(ret))
			return ret;
	}
	else
		(void) sm_strprintf(str, "%ld/", (long) rl.rlim_cur);
	if (RLIM_INFINITY == rl.rlim_max)
	{
		ret = sm_str_scat(str, "unlimited\n");
		if (sm_is_err(ret))
			return ret;
	}
	else
		(void) sm_strprintf(str, "%ld\n", (long) rl.rlim_max);
	return ret;
}
#endif /* HAVE_GETRLIMIT */

/*
**  SM_PRTRLIMITS -- print rlimits to str
**
**	Parameters:
**		str -- string for output
**
**	Returns:
**		usual sm_error code
*/

sm_ret_T
sm_prtrlimits(sm_str_P str)
{
#if HAVE_GETRLIMIT
	sm_ret_T ret;
	int r;
	struct rlimit rl;

#define GETANDPRT(resource, name)	\
	do {				\
		r = getrlimit(resource, &rl);	\
		if (0 == r)			\
			ret = sm_prtrlimit(rl, name, str);	\
	} while (0)

	SM_REQUIRE(str != NULL);
	ret = SM_SUCCESS;
#ifdef RLIMIT_CPU
	GETANDPRT(RLIMIT_CPU, "cpu=");
#endif
#ifdef RLIMIT_DATA
	GETANDPRT(RLIMIT_DATA, "data=");
#endif
#ifdef RLIMIT_FSIZE
	GETANDPRT(RLIMIT_FSIZE, "filesize=");
#endif
#ifdef RLIMIT_RSS
	GETANDPRT(RLIMIT_RSS, "memory=");
#endif
#ifdef RLIMIT_AS
	GETANDPRT(RLIMIT_AS, "avail_memory=");
#endif
#ifdef RLIMIT_VMEM
	GETANDPRT(RLIMIT_VMEM, "vmem=");
#endif
#ifdef RLIMIT_STACK
	GETANDPRT(RLIMIT_STACK, "stack=");
#endif
#ifdef RLIMIT_CORE
	GETANDPRT(RLIMIT_CORE, "core=");
#endif
#ifdef RLIMIT_NOFILE
	GETANDPRT(RLIMIT_NOFILE, "#files=");
#endif

	return ret;
#else /* HAVE_GETRLIMIT */
	return SM_SUCCESS;
#endif /* HAVE_GETRLIMIT */
}


syntax highlighted by Code2HTML, v. 0.9.1