Skip to content

Commit 9b60c4d

Browse files
authored
Merge pull request #5746 from btriller/macos-env
Define macos and additional version hardclasses
2 parents 07776e1 + b0bcd20 commit 9b60c4d

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);
@@ -1179,7 +1183,7 @@ static void OSReleaseParse(EvalContext *ctx, const char *file_path)
11791183
{
11801184
alias = "redhat";
11811185
}
1182-
else if (StringEqual(os_release_id, "opensuse") ||
1186+
else if (StringEqual(os_release_id, "opensuse") ||
11831187
StringEqual(os_release_id, "sles"))
11841188
{
11851189
alias = "suse";
@@ -1410,6 +1414,10 @@ static void OSClasses(EvalContext *ctx)
14101414

14111415
#else
14121416

1417+
#ifdef __APPLE__
1418+
Apple_Version(ctx);
1419+
#endif
1420+
14131421
char vbuff[CF_MAXVARSIZE];
14141422

14151423
#ifdef _AIX
@@ -1419,6 +1427,7 @@ static void OSClasses(EvalContext *ctx)
14191427
#endif
14201428

14211429

1430+
#ifndef __APPLE__
14221431
for (char *sp = vbuff; *sp != '\0'; sp++)
14231432
{
14241433
if (*sp == '-')
@@ -1431,6 +1440,7 @@ static void OSClasses(EvalContext *ctx)
14311440
char context[CF_BUFSIZE];
14321441
snprintf(context, CF_BUFSIZE, "%s_%s", VSYSNAME.sysname, vbuff);
14331442
SetFlavor(ctx, context);
1443+
#endif
14341444

14351445

14361446
#ifdef __hpux
@@ -1593,6 +1603,10 @@ static void OSClasses(EvalContext *ctx)
15931603
{
15941604
snprintf(vbuff, CF_BUFSIZE, "/var/cron/tabs/%s", user_name);
15951605
}
1606+
else if (EvalContextClassGet(ctx, NULL, "macos"))
1607+
{
1608+
snprintf(vbuff, CF_BUFSIZE, "/usr/lib/cron/tabs/%s", user_name);
1609+
}
15961610
else
15971611
{
15981612
snprintf(vbuff, CF_BUFSIZE, "/var/spool/cron/crontabs/%s", user_name);
@@ -1641,6 +1655,93 @@ static void OSClasses(EvalContext *ctx)
16411655

16421656
/*********************************************************************************/
16431657

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

35843685
/**
35853686
* Find next integer from string in place. Leading zero's are included.
3586-
*
3687+
*
35873688
* @param [in] str string to extract next integer from
35883689
* @param [out] num pointer to start of next integer or %NULL if no integer
35893690
* number was found
3590-
*
3691+
*
35913692
* @return pointer to the remaining string in `str` or %NULL if no remainder
3592-
*
3693+
*
35933694
* @note `str` will be mutated
35943695
*/
35953696
static char *FindNextInteger(char *str, char **num)
@@ -3657,8 +3758,8 @@ static void SysOsVersionMajor(EvalContext *ctx)
36573758
}
36583759
else
36593760
{
3660-
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS,
3661-
"os_version_major", major,
3761+
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS,
3762+
"os_version_major", major,
36623763
CF_DATA_TYPE_STRING,
36633764
"source=agent,derived-from=flavor");
36643765
}

0 commit comments

Comments
 (0)