Blame Esm/ib/src/ibaccess/vs_utility.c

Packit 857059
/* BEGIN_ICS_COPYRIGHT5 ****************************************
Packit 857059
Packit 857059
Copyright (c) 2015-2017, Intel Corporation
Packit 857059
Packit 857059
Redistribution and use in source and binary forms, with or without
Packit 857059
modification, are permitted provided that the following conditions are met:
Packit 857059
Packit 857059
    * Redistributions of source code must retain the above copyright notice,
Packit 857059
      this list of conditions and the following disclaimer.
Packit 857059
    * Redistributions in binary form must reproduce the above copyright
Packit 857059
      notice, this list of conditions and the following disclaimer in the
Packit 857059
      documentation and/or other materials provided with the distribution.
Packit 857059
    * Neither the name of Intel Corporation nor the names of its contributors
Packit 857059
      may be used to endorse or promote products derived from this software
Packit 857059
      without specific prior written permission.
Packit 857059
Packit 857059
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 857059
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 857059
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit 857059
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
Packit 857059
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 857059
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit 857059
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Packit 857059
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit 857059
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 857059
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 857059
Packit 857059
 * ** END_ICS_COPYRIGHT5   ****************************************/
Packit 857059
Packit 857059
//=======================================================================
Packit 857059
//									/
Packit 857059
// FILE NAME								/
Packit 857059
//    vs_utility.c							/
Packit 857059
//									/
Packit 857059
// DESCRIPTION								/
Packit 857059
//    This is the CS routines which don't fit a common category.	/
Packit 857059
//									/
Packit 857059
// DATA STRUCTURES							/
Packit 857059
//    None								/
Packit 857059
//									/
Packit 857059
// FUNCTIONS								/
Packit 857059
//    vs_enter		log an entry into a routine			/
Packit 857059
//    vs_exit		log an exit from a routine			/
Packit 857059
//    vs_time_get	get the current epoch time			/
Packit 857059
//									/
Packit 857059
// DEPENDENCIES								/
Packit 857059
//    ib_mad.h								/
Packit 857059
//    ib_status.h							/
Packit 857059
//									/
Packit 857059
//									/
Packit 857059
// HISTORY								/
Packit 857059
//									/
Packit 857059
//    NAME	DATE  REMARKS						/
Packit 857059
//     jsy  02/14/01  Initial creation of file.				/
Packit 857059
//     jsy  04/04/01  Added vs_getpid function.				/
Packit 857059
//     trp  08/20/01  Removed vs_getpid (vs_thread_name) is one to use	/
Packit 857059
//=======================================================================
Packit 857059
#include <stdio.h>
Packit 857059
#include <errno.h>
Packit 857059
#include <signal.h>
Packit 857059
#include <string.h>
Packit 857059
#include <sys/stat.h>
Packit 857059
#include <unistd.h>
Packit 857059
#include <pthread.h>
Packit 857059
#include <sys/time.h>
Packit 857059
#include <sys/resource.h>
Packit 857059
#include "ib_mad.h"
Packit 857059
#include "ib_status.h"
Packit 857059
#include "cs_g.h"
Packit 857059
#include "cs_log.h"
Packit 857059
#include <features.h>
Packit 857059
#ifdef LOCAL_MOD_ID
Packit 857059
#undef LOCAL_MOD_ID
Packit 857059
#endif
Packit 857059
#define LOCAL_MOD_ID VIEO_CS_MOD_ID
Packit 857059
Packit 857059
//----------------------------------------------------------------------------//
Packit 857059
Packit 857059
Status_t
Packit 857059
vs_time_get(uint64_t *address)
Packit 857059
{
Packit 857059
	uint64_t	usecs;
Packit 857059
	struct	timespec	now;
Packit 857059
Packit 857059
        if (address == 0)
Packit 857059
          {
Packit 857059
            return(VSTATUS_ILLPARM);
Packit 857059
          }
Packit 857059
	clock_gettime(CLOCK_MONOTONIC, &now;;
Packit 857059
	usecs = now.tv_sec;
Packit 857059
	usecs *= 1000000;
Packit 857059
	usecs += (now.tv_nsec/1000);
Packit 857059
	/*round up anything greater than 0.5 usecs*/
Packit 857059
	if (now.tv_nsec % 1000 > 500)
Packit 857059
		usecs++;
Packit 857059
Packit 857059
	*address = usecs;
Packit 857059
Packit 857059
	return(VSTATUS_OK);
Packit 857059
}
Packit 857059
Packit 857059
/*
Packit 857059
 * vs_stdtime_get - get current time as a time_t, seconds since 1970
Packit 857059
 */
Packit 857059
Status_t
Packit 857059
vs_stdtime_get(time_t *address)
Packit 857059
{
Packit 857059
	if (-1 == time(address))
Packit 857059
		return VSTATUS_BAD;
Packit 857059
	return(VSTATUS_OK);
Packit 857059
}
Packit 857059
Packit 857059
// Translate CoreDumpLimit string to a rlimit value
Packit 857059
// returns 0 on success, -1 on error
Packit 857059
// value argument is optional, if NULL just checks str is valid CoreDumpLimit
Packit 857059
// str can be a byte count or "unlimited"
Packit 857059
int vs_getCoreDumpLimit(const char* str, uint64_t* value)
Packit 857059
{
Packit 857059
	uint64_t rlimit;
Packit 857059
Packit 857059
	if (!str)
Packit 857059
		return -1;
Packit 857059
Packit 857059
	if (! strlen(str))
Packit 857059
		return -1;
Packit 857059
Packit 857059
	if (strcasecmp(str, "unlimited") == 0) {
Packit 857059
		rlimit = RLIM_INFINITY;
Packit 857059
	} else {
Packit 857059
		if (FSUCCESS != StringToUint64Bytes(&rlimit, str, NULL, 0, TRUE))
Packit 857059
			return -1;
Packit 857059
		// treat small values as Megabytes
Packit 857059
		if (rlimit < 8192)
Packit 857059
			rlimit = rlimit * (1024*1024);
Packit 857059
	}
Packit 857059
Packit 857059
	if (value)
Packit 857059
		*value = rlimit;
Packit 857059
	return 0;
Packit 857059
}
Packit 857059
Packit 857059
/*
Packit 857059
 *  vs_init_coredump_settings - setup coredump config for this process
Packit 857059
 *  note this changes the current directory
Packit 857059
 */
Packit 857059
void vs_init_coredump_settings(const char* mgr, const char* limit, const char *dir)
Packit 857059
{
Packit 857059
	struct rlimit rlimit;
Packit 857059
	char buf[140];
Packit 857059
	struct stat statbuf;
Packit 857059
   
Packit 857059
	rlimit.rlim_max = RLIM_SAVED_MAX;
Packit 857059
	if (0 != vs_getCoreDumpLimit(limit, &rlimit.rlim_cur)) {
Packit 857059
		// parser should have already validated, but just to be safe
Packit 857059
		snprintf(buf, sizeof(buf), "%s: Disabling CoreDumps: Invalid CoreDumpLimit: '%s'",
Packit 857059
				mgr, limit);
Packit 857059
		vs_log_output_message(buf, FALSE);
Packit 857059
		rlimit.rlim_cur = 0; limit = "0";
Packit 857059
	} else if (rlimit.rlim_cur == 0) {
Packit 857059
		snprintf(buf, sizeof(buf), "%s: Disabling CoreDumps: CoreDumpLimit: %s", mgr, limit);
Packit 857059
		vs_log_output_message(buf, FALSE);
Packit 857059
	} else if (strlen(dir) == 0 || 0 == strcmp(dir, "/dev/null")) {
Packit 857059
		snprintf(buf, sizeof(buf), "%s: Disabling CoreDumps: No CoreDumpDir", mgr);
Packit 857059
		vs_log_output_message(buf, FALSE);
Packit 857059
		rlimit.rlim_cur = 0; limit = "0";
Packit 857059
	} else {
Packit 857059
		if (-1 == stat(dir, &statbuf)) {
Packit 857059
			if (mkdir(dir, 0644) != 0) {
Packit 857059
				snprintf(buf, sizeof(buf), "%s: Disabling CoreDumps: Unable to create CoreDumpDir: '%s' %m", mgr, dir);
Packit 857059
				vs_log_output_message(buf, FALSE);
Packit 857059
				rlimit.rlim_cur = 0; limit = "0";
Packit 857059
			}
Packit 857059
		} else if (! S_ISDIR(statbuf.st_mode)) {
Packit 857059
			snprintf(buf, sizeof(buf), "%s: Disabling CoreDumps: CoreDumpDir is not a directory: '%s'", mgr, dir);
Packit 857059
			vs_log_output_message(buf, FALSE);
Packit 857059
			rlimit.rlim_cur = 0; limit = "0";
Packit 857059
		}
Packit 857059
		if (0 != chdir(dir)) {
Packit 857059
			snprintf(buf, sizeof(buf), "%s: Disabling CoreDumps: Unable to cd CoreDumpDir: '%s' %m", mgr, dir);
Packit 857059
			vs_log_output_message(buf, FALSE);
Packit 857059
			rlimit.rlim_cur = 0; limit = "0";
Packit 857059
		}
Packit 857059
	}
Packit 857059
	if (setrlimit(RLIMIT_CORE, &rlimit) != 0) {
Packit 857059
		snprintf(buf, sizeof(buf), "%s: Unable to change CoreDumpLimit to '%s' %m", mgr, limit);
Packit 857059
		vs_log_output_message(buf, FALSE);
Packit 857059
	} else if (rlimit.rlim_cur) {
Packit 857059
		snprintf(buf, sizeof(buf), "%s: Enabling CoreDumps to %s up to %s bytes", mgr, dir, limit);
Packit 857059
		vs_log_output_message(buf, FALSE);
Packit 857059
	}
Packit 857059
}