Skip to content

Commit b0bcd20

Browse files
committed
Define macos and additional version hardclasses
Fix sys.os_name_human warning on macOS Operating System not properly recognized, setting sys.os_name_human to "Unknown", please submit a bug report for us to fix this due to missing "macos" hardclass. Define expected macos hardclass and additional major, minor and patch version hardclasses obtained from sw_vers tool. This changes sys.flavor from darwin_KERNEL_VER to macos_MACOS_MAJOR_VER.
1 parent 8788bcf commit b0bcd20

File tree

1 file changed

+107
-6
lines changed

1 file changed

+107
-6
lines changed

libenv/sysinfo.c

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ void CalculateDomainName(const char *nodename, const char *dnsname,
161161
char *uqname, size_t uqname_size,
162162
char *domain, size_t domain_size);
163163

164+
#ifdef __APPLE__
165+
static void Apple_Version(EvalContext *ctx);
166+
#endif
167+
164168
#ifdef __linux__
165169
static int Linux_Fedora_Version(EvalContext *ctx);
166170
static int Linux_Redhat_Version(EvalContext *ctx);
@@ -1183,7 +1187,7 @@ static void OSReleaseParse(EvalContext *ctx, const char *file_path)
11831187
{
11841188
alias = "redhat";
11851189
}
1186-
else if (StringEqual(os_release_id, "opensuse") ||
1190+
else if (StringEqual(os_release_id, "opensuse") ||
11871191
StringEqual(os_release_id, "sles"))
11881192
{
11891193
alias = "suse";
@@ -1414,6 +1418,10 @@ static void OSClasses(EvalContext *ctx)
14141418

14151419
#else
14161420

1421+
#ifdef __APPLE__
1422+
Apple_Version(ctx);
1423+
#endif
1424+
14171425
char vbuff[CF_MAXVARSIZE];
14181426

14191427
#ifdef _AIX
@@ -1423,6 +1431,7 @@ static void OSClasses(EvalContext *ctx)
14231431
#endif
14241432

14251433

1434+
#ifndef __APPLE__
14261435
for (char *sp = vbuff; *sp != '\0'; sp++)
14271436
{
14281437
if (*sp == '-')
@@ -1435,6 +1444,7 @@ static void OSClasses(EvalContext *ctx)
14351444
char context[CF_BUFSIZE];
14361445
snprintf(context, CF_BUFSIZE, "%s_%s", VSYSNAME.sysname, vbuff);
14371446
SetFlavor(ctx, context);
1447+
#endif
14381448

14391449

14401450
#ifdef __hpux
@@ -1597,6 +1607,10 @@ static void OSClasses(EvalContext *ctx)
15971607
{
15981608
snprintf(vbuff, CF_BUFSIZE, "/var/cron/tabs/%s", user_name);
15991609
}
1610+
else if (EvalContextClassGet(ctx, NULL, "macos"))
1611+
{
1612+
snprintf(vbuff, CF_BUFSIZE, "/usr/lib/cron/tabs/%s", user_name);
1613+
}
16001614
else
16011615
{
16021616
snprintf(vbuff, CF_BUFSIZE, "/var/spool/cron/crontabs/%s", user_name);
@@ -1645,6 +1659,93 @@ static void OSClasses(EvalContext *ctx)
16451659

16461660
/*********************************************************************************/
16471661

1662+
#ifdef __APPLE__
1663+
static void Apple_Version(EvalContext *ctx)
1664+
{
1665+
FILE *pp = NULL;
1666+
1667+
Log(LOG_LEVEL_VERBOSE, "This appears to be an apple system.");
1668+
Log(LOG_LEVEL_VERBOSE, "Looking for product name and version...");
1669+
if ((!FileCanOpen("/usr/bin/sw_vers", "r") || ((pp = cf_popen("/usr/bin/sw_vers", "r", true)) == NULL)))
1670+
{
1671+
Log(LOG_LEVEL_ERR, "Could not open and run /usr/bin/sw_vers to find macOS system version information.");
1672+
return;
1673+
}
1674+
1675+
size_t line_size = CF_BUFSIZE;
1676+
char *line = xmalloc(line_size);
1677+
int revcomps = 0;
1678+
unsigned int major, minor, patch;
1679+
char *flavor = NULL, *product_name = NULL, *r;
1680+
1681+
while (CfReadLine(&line, &line_size, pp) != -1)
1682+
{
1683+
if (STARTSWITH(line, "ProductName:"))
1684+
{
1685+
r = strrchr(line, '\t');
1686+
if (r == NULL || ++r == NULL)
1687+
{
1688+
continue;
1689+
}
1690+
product_name = SafeStringDuplicate(r);
1691+
ToLowerStrInplace(r);
1692+
flavor = SafeStringDuplicate(r);
1693+
EvalContextClassPutHard(
1694+
ctx,
1695+
r,
1696+
"inventory,attribute_name=none,source=agent,derived-from=sw_vers");
1697+
}
1698+
else if (STARTSWITH(line, "ProductVersion:"))
1699+
{
1700+
r = strrchr(line, '\t');
1701+
if (r == NULL || ++r == NULL)
1702+
{
1703+
continue;
1704+
}
1705+
revcomps = sscanf(r, "%u.%u.%u", &major, &minor, &patch);
1706+
}
1707+
}
1708+
1709+
if (flavor == NULL)
1710+
{
1711+
free(line);
1712+
cf_pclose(pp);
1713+
return;
1714+
}
1715+
1716+
char buf[CF_BUFSIZE];
1717+
1718+
if (revcomps > 0)
1719+
{
1720+
NDEBUG_UNUSED int ret = snprintf(buf, sizeof(buf), "%s_%u", flavor, major);
1721+
assert(ret >= 0 && (size_t) ret < sizeof(buf));
1722+
Log(LOG_LEVEL_VERBOSE, "This appears to be a %s %u system.", product_name, major);
1723+
SetFlavor(ctx, buf);
1724+
}
1725+
1726+
if (revcomps > 1)
1727+
{
1728+
NDEBUG_UNUSED int ret = snprintf(buf, sizeof(buf), "%s_%u_%u", flavor, major, minor);
1729+
assert(ret >= 0 && (size_t) ret < sizeof(buf));
1730+
Log(LOG_LEVEL_VERBOSE, "This appears to be a %s %u.%u system.", product_name, major, minor);
1731+
EvalContextClassPutHard(ctx, buf, "inventory,attribute_name=none,source=agent");
1732+
}
1733+
1734+
if (revcomps > 2)
1735+
{
1736+
NDEBUG_UNUSED int ret = snprintf(buf, sizeof(buf), "%s_%u_%u_%u", flavor, major, minor, patch);
1737+
assert(ret >= 0 && (size_t) ret < sizeof(buf));
1738+
Log(LOG_LEVEL_VERBOSE, "This appears to be a %s %u.%u.%u system.", product_name, major, minor, patch);
1739+
EvalContextClassPutHard(ctx, buf, "inventory,attribute_name=none,source=agent");
1740+
}
1741+
1742+
free(line);
1743+
free(flavor);
1744+
free(product_name);
1745+
cf_pclose(pp);
1746+
}
1747+
#endif
1748+
16481749
#ifdef __linux__
16491750
static void Linux_Oracle_VM_Server_Version(EvalContext *ctx)
16501751
{
@@ -3587,13 +3688,13 @@ static void SysOSNameHuman(EvalContext *ctx)
35873688

35883689
/**
35893690
* Find next integer from string in place. Leading zero's are included.
3590-
*
3691+
*
35913692
* @param [in] str string to extract next integer from
35923693
* @param [out] num pointer to start of next integer or %NULL if no integer
35933694
* number was found
3594-
*
3695+
*
35953696
* @return pointer to the remaining string in `str` or %NULL if no remainder
3596-
*
3697+
*
35973698
* @note `str` will be mutated
35983699
*/
35993700
static char *FindNextInteger(char *str, char **num)
@@ -3661,8 +3762,8 @@ static void SysOsVersionMajor(EvalContext *ctx)
36613762
}
36623763
else
36633764
{
3664-
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS,
3665-
"os_version_major", major,
3765+
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS,
3766+
"os_version_major", major,
36663767
CF_DATA_TYPE_STRING,
36673768
"source=agent,derived-from=flavor");
36683769
}

0 commit comments

Comments
 (0)