Skip to content

Commit 628055f

Browse files
committed
Move GetRelocatedProcdirRoot() from libntech and add GetProcdirPid()
These have checks for environment variables to override to support testing. Ticket: CFE-3429 Changelog: none
1 parent 8441e84 commit 628055f

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

libenv/unix_iface.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <ip_address.h>
4040
#include <file_lib.h>
4141
#include <cleanup.h>
42+
#include <unix.h> /* GetRelocatedProcdirRoot() and GetProcdirPid() */
4243

4344
#ifdef HAVE_SYS_JAIL_H
4445
# include <sys/jail.h>

libpromises/unix.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <unix.h>
2626
#include <exec_tools.h>
2727
#include <file_lib.h>
28+
#include <string_lib.h> /* StringToInt64() */
2829

2930
#ifdef HAVE_SYS_UIO_H
3031
# include <sys/uio.h>
@@ -527,4 +528,45 @@ bool GetGroupID(const char *group_name, gid_t *gid, LogLevel error_log_level)
527528
return true;
528529
}
529530

531+
const char* GetRelocatedProcdirRoot()
532+
{
533+
const char *procdir = getenv("CFENGINE_TEST_OVERRIDE_PROCDIR");
534+
if (procdir == NULL)
535+
{
536+
procdir = "";
537+
}
538+
else
539+
{
540+
Log(LOG_LEVEL_VERBOSE, "Overriding /proc location to be %s", procdir);
541+
}
542+
543+
return procdir;
544+
}
545+
546+
/**
547+
* With the addition of using the current PID when examining the proc filesystem for details
548+
* we need a test-override option for acceptance tests.
549+
*/
550+
int GetProcdirPid()
551+
{
552+
const char *procpid = getenv("CFENGINE_TEST_OVERRIDE_PROCPID");
553+
if (procpid != NULL)
554+
{
555+
int64_t pid_int;
556+
int rc;
557+
rc = StringToInt64(procpid, &pid_int);
558+
if (rc != 0)
559+
{
560+
Log(LOG_LEVEL_ERR, "Could not parse CFENGINE_TEST_OVERRIDE_PROCPID '%s' as integer: '%s'", procpid, strerror(errno));
561+
}
562+
else
563+
{
564+
Log(LOG_LEVEL_VERBOSE, "Overriding proc pid from env var CFENGINE_TEST_OVERRIDE_PROCPID with value %jd", (intmax_t) pid_int);
565+
return (int) pid_int;
566+
}
567+
}
568+
return (int) getpid();
569+
}
570+
571+
530572
#endif /* !__MINGW32__ */

libpromises/unix.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ void ProcessSignalTerminate(pid_t pid);
3333
bool GetCurrentUserName(char *userName, int userNameLen);
3434

3535
#ifndef __MINGW32__
36+
/**
37+
* @brief For testing things against /proc, uses env var CFENGINE_TEST_OVERRIDE_PROCDIR
38+
* @return the extra directory to add BEFORE /proc in the path
39+
*/
40+
const char* GetRelocatedProcdirRoot();
41+
/**
42+
* @brief For testing things against /proc, use env var CFENGINE_TEST_OVERRIDE_PROCPID
43+
* @return the fake pid to use in proc paths
44+
*/
45+
int GetProcdirPid();
46+
3647
/**
3748
* Get user name for the user with UID #uid
3849
*

0 commit comments

Comments
 (0)