Skip to content

Commit 9d97d6e

Browse files
committed
Added os_version_minor sys variable
Ticket: ENT-8118 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 90e7ea1 commit 9d97d6e

File tree

1 file changed

+154
-2
lines changed

1 file changed

+154
-2
lines changed

libenv/sysinfo.c

Lines changed: 154 additions & 2 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+
static void SysOSVersionMinorPut(EvalContext *ctx, char *strminor, char *tags);
165+
static void SysOSVersionMinorPutInt(EvalContext *ctx, int minor, char *tags);
166+
static void SysOsVersionMinorSetDefault(EvalContext *ctx);
167+
164168
#ifdef __APPLE__
165169
static void Apple_Version(EvalContext *ctx);
166170
#endif
@@ -1157,6 +1161,30 @@ static void DefineVersionedHardClasses(
11571161
free(class);
11581162
}
11591163

1164+
static void SysOSVersionMinorPutFromVersion(EvalContext *ctx, char *name, char *version_id, char *tags)
1165+
{
1166+
assert(version_id != NULL);
1167+
Item *version_tuple = SplitString(version_id, '_');
1168+
1169+
if (version_tuple == NULL)
1170+
{
1171+
return;
1172+
}
1173+
if (name != NULL && (StringStartsWith(name, "solaris") || StringStartsWith(name, "sunos")))
1174+
{
1175+
SysOSVersionMinorPut(ctx, version_tuple->name, tags);
1176+
DeleteItemList(version_tuple);
1177+
return;
1178+
}
1179+
if (version_tuple->next == NULL)
1180+
{
1181+
DeleteItemList(version_tuple);
1182+
return;
1183+
}
1184+
SysOSVersionMinorPut(ctx, version_tuple->next->name, tags);
1185+
DeleteItemList(version_tuple);
1186+
}
1187+
11601188
static void OSReleaseParse(EvalContext *ctx, const char *file_path)
11611189
{
11621190
JsonElement *os_release_json = JsonReadDataFile("system info discovery",
@@ -1213,6 +1241,12 @@ static void OSReleaseParse(EvalContext *ctx, const char *file_path)
12131241
*first_underscore = '\0';
12141242
SetFlavor2(ctx, os_release_id, os_release_version_id);
12151243
*first_underscore = '_';
1244+
1245+
// set os version minor
1246+
const char *const_os_release_name = JsonObjectGetAsString(os_release_json, "NAME");
1247+
char *os_release_name = SafeStringDuplicate(const_os_release_name);
1248+
SysOSVersionMinorPutFromVersion(ctx, os_release_name, os_release_version_id, tags);
1249+
free(os_release_name);
12161250
}
12171251
else
12181252
{
@@ -1461,12 +1495,29 @@ static void OSClasses(EvalContext *ctx)
14611495

14621496
// Extract major version number
14631497
char *major = NULL;
1464-
for (char *sp = vbuff; *sp != '\0'; sp++)
1498+
char *minor = NULL;
1499+
char *sp = vbuff;
1500+
for (sp; *sp != '\0'; sp++)
14651501
{
14661502
if (major == NULL && isdigit(*sp))
14671503
{
14681504
major = sp;
14691505
}
1506+
if (major == NULL && !isdigit(*sp))
1507+
{
1508+
*sp = '\0';
1509+
}
1510+
if (major != NULL && !isdigit(*sp))
1511+
{
1512+
break;
1513+
}
1514+
}
1515+
for (sp; *sp != '\0'; sp++)
1516+
{
1517+
if (minor == NULL && isdigit(*sp))
1518+
{
1519+
minor = sp;
1520+
}
14701521
else if (!isdigit(*sp))
14711522
{
14721523
*sp = '\0';
@@ -1478,6 +1529,10 @@ static void OSClasses(EvalContext *ctx)
14781529
snprintf(context, CF_BUFSIZE, "hpux_%s", major);
14791530
EvalContextClassPutHard(ctx, context, "source=agent,derived-from=sys.flavor");
14801531
}
1532+
if (minor != NULL)
1533+
{
1534+
SysOSVersionMinorPut(ctx, minor, "source=agent");
1535+
}
14811536
#endif
14821537

14831538
#ifdef __FreeBSD__
@@ -1487,17 +1542,24 @@ static void OSClasses(EvalContext *ctx)
14871542
* For example, when being run on either FreeBSD 10.0 or 10.1 a class
14881543
* called freebsd_10 will be defined
14891544
*/
1490-
for (char *sp = vbuff; *sp != '\0'; sp++)
1545+
char *sp = NULL;
1546+
for (sp = vbuff; *sp != '\0'; sp++)
14911547
{
14921548
if (*sp == '.')
14931549
{
14941550
*sp = '\0';
14951551
break;
14961552
}
14971553
}
1554+
sp++;
1555+
char *minor = sp;
14981556

14991557
snprintf(context, CF_BUFSIZE, "%s_%s", VSYSNAME.sysname, vbuff);
15001558
EvalContextClassPutHard(ctx, context, "source=agent,derived-from=sys.flavor");
1559+
if (minor != NULL)
1560+
{
1561+
SysOSVersionMinorPut(ctx, minor, "source=agent");
1562+
}
15011563
#endif
15021564

15031565
#endif
@@ -1524,30 +1586,35 @@ static void OSClasses(EvalContext *ctx)
15241586
{
15251587
Log(LOG_LEVEL_VERBOSE, "This appears to be Windows 2000");
15261588
EvalContextClassPutHard(ctx, "Win2000", "inventory,attribute_name=none,source=agent");
1589+
SysOSVersionMinorPut(ctx, "5.0", "source=agent");
15271590
}
15281591

15291592
if (strncmp(sp, "5.1", 3) == 0)
15301593
{
15311594
Log(LOG_LEVEL_VERBOSE, "This appears to be Windows XP");
15321595
EvalContextClassPutHard(ctx, "WinXP", "inventory,attribute_name=none,source=agent");
1596+
SysOSVersionMinorPut(ctx, "5.1", "source=agent");
15331597
}
15341598

15351599
if (strncmp(sp, "5.2", 3) == 0)
15361600
{
15371601
Log(LOG_LEVEL_VERBOSE, "This appears to be Windows Server 2003");
15381602
EvalContextClassPutHard(ctx, "WinServer2003", "inventory,attribute_name=none,source=agent");
1603+
SysOSVersionMinorPut(ctx, "5.2", "source=agent");
15391604
}
15401605

15411606
if (strncmp(sp, "6.1", 3) == 0)
15421607
{
15431608
Log(LOG_LEVEL_VERBOSE, "This appears to be Windows Vista");
15441609
EvalContextClassPutHard(ctx, "WinVista", "inventory,attribute_name=none,source=agent");
1610+
SysOSVersionMinorPut(ctx, "56.1", "source=agent");
15451611
}
15461612

15471613
if (strncmp(sp, "6.3", 3) == 0)
15481614
{
15491615
Log(LOG_LEVEL_VERBOSE, "This appears to be Windows Server 2008");
15501616
EvalContextClassPutHard(ctx, "WinServer2008", "inventory,attribute_name=none,source=agent");
1617+
SysOSVersionMinorPut(ctx, "6.3", "source=agent");
15511618
}
15521619
}
15531620
}
@@ -1588,6 +1655,27 @@ static void OSClasses(EvalContext *ctx)
15881655
else
15891656
{
15901657
SetFlavor2(ctx, "windows", major);
1658+
1659+
// minor version
1660+
char *minor = NULL;
1661+
int idx = (major - release) + strlen(major) + 1;
1662+
char *sp = &release[idx];
1663+
1664+
for (sp; *sp != '\0'; sp++)
1665+
{
1666+
if (minor == NULL && isdigit(*sp))
1667+
{
1668+
minor = sp;
1669+
}
1670+
if ((*sp != '.' || *sp != '_') && !isdigit(*sp))
1671+
{
1672+
*sp = '\0';
1673+
}
1674+
}
1675+
if (minor != NULL)
1676+
{
1677+
SysOSVersionMinorPut(ctx, minor, "source=agent");
1678+
}
15911679
}
15921680
free(release);
15931681

@@ -1733,6 +1821,7 @@ static void Apple_Version(EvalContext *ctx)
17331821
assert(ret >= 0 && (size_t) ret < sizeof(buf));
17341822
Log(LOG_LEVEL_VERBOSE, "This appears to be a %s %u.%u system.", product_name, major, minor);
17351823
EvalContextClassPutHard(ctx, buf, "inventory,attribute_name=none,source=agent");
1824+
SysOSVersionMinorPutInt(ctx, minor, "source=agent");
17361825
}
17371826

17381827
if (revcomps > 2)
@@ -1798,6 +1887,7 @@ static void Linux_Oracle_VM_Server_Version(EvalContext *ctx)
17981887

17991888
snprintf(buf, CF_BUFSIZE, "oraclevmserver_%d_%d", major, minor);
18001889
EvalContextClassPutHard(ctx, buf, "inventory,attribute_name=none,source=agent");
1890+
SysOSVersionMinorPutInt(ctx, minor, "source=agent");
18011891
}
18021892

18031893
if (revcomps > 2)
@@ -1849,6 +1939,7 @@ static void Linux_Oracle_Version(EvalContext *ctx)
18491939

18501940
snprintf(buf, CF_BUFSIZE, "oracle_%d_%d", major, minor);
18511941
EvalContextClassPutHard(ctx, buf, "inventory,attribute_name=none,source=agent");
1942+
SysOSVersionMinorPutInt(ctx, minor, "source=agent");
18521943
}
18531944
}
18541945

@@ -2164,6 +2255,11 @@ static int Linux_Redhat_Version(EvalContext *ctx)
21642255
}
21652256
}
21662257

2258+
if (minor >= 0)
2259+
{
2260+
SysOSVersionMinorPutInt(ctx, minor, "source=agent,derived-from-file="RH_REL_FILENAME);
2261+
}
2262+
21672263
return 0;
21682264
}
21692265

@@ -2382,6 +2478,11 @@ static int Linux_Suse_Version(EvalContext *ctx)
23822478
}
23832479
}
23842480

2481+
if (minor >= 0)
2482+
{
2483+
SysOSVersionMinorPutInt(ctx, minor, "source=agent");
2484+
}
2485+
23852486
Log(LOG_LEVEL_VERBOSE, "Could not find a numeric OS release in %s", SUSE_REL_FILENAME);
23862487

23872488
return 0;
@@ -2417,6 +2518,7 @@ static int Linux_Slackware_Version(EvalContext *ctx, char *filename)
24172518
Log(LOG_LEVEL_VERBOSE, "This appears to be a Slackware %u.%u system.", major, minor);
24182519
snprintf(classname, CF_MAXVARSIZE, "slackware_%u_%u", major, minor);
24192520
EvalContextClassPutHard(ctx, classname, "inventory,attribute_name=none,source=agent");
2521+
SysOSVersionMinorPutInt(ctx, minor, "source=agent");
24202522
/* Fall-through */
24212523
case 1:
24222524
Log(LOG_LEVEL_VERBOSE, "This appears to be a Slackware %u system.", major);
@@ -2506,6 +2608,7 @@ static int Linux_Misc_Version(EvalContext *ctx)
25062608
nt_static_assert((sizeof(version)) > 255);
25072609
sscanf(sp + strlen("DISTRIB_RELEASE="), "%255[^\n]", version);
25082610
CanonifyNameInPlace(version);
2611+
SysOSVersionMinorPutFromVersion(ctx, "", version, "source=agent, derived-from=/etc/lsb_release");
25092612
}
25102613
}
25112614
fclose(fp);
@@ -2624,6 +2727,10 @@ static int Linux_Debian_Version(EvalContext *ctx)
26242727
"inventory,attribute_name=none,source=agent,derived-from-file="DEBIAN_ISSUE_FILENAME);
26252728
}
26262729
}
2730+
if (release >= 0)
2731+
{
2732+
SysOSVersionMinorPutInt(ctx, release, "source=agent,derived-from-file="DEBIAN_ISSUE_FILENAME);
2733+
}
26272734

26282735
return 0;
26292736
}
@@ -2755,6 +2862,8 @@ static int Linux_Mandriva_Version_Real(EvalContext *ctx, char *filename, char *r
27552862
strcat(classbuf, "_");
27562863
strcat(classbuf, strminor);
27572864
EvalContextClassPutHard(ctx, classbuf, "inventory,attribute_name=none,source=agent");
2865+
2866+
SysOSVersionMinorPutInt(ctx, minor, "source=agent");
27582867
}
27592868
}
27602869

@@ -2820,6 +2929,7 @@ static void Linux_Alpine_Version(EvalContext *ctx)
28202929
{
28212930
char class[CF_MAXVARSIZE];
28222931
CanonifyNameInPlace(version);
2932+
SysOSVersionMinorPutFromVersion(ctx, "alpine_linux", version, "source=agent,derived-from-file=/etc/alpine-release");
28232933
snprintf(class, sizeof(class), "alpine_linux_%s", version);
28242934
EvalContextClassPutHard(ctx, class,
28252935
"inventory,attribute_name=none,source=agent"
@@ -2848,6 +2958,7 @@ static int EOS_Version(EvalContext *ctx)
28482958
version[0] = '\0';
28492959
sscanf(buffer, "%*s %*s %*s %255s", version);
28502960
CanonifyNameInPlace(version);
2961+
SysOSVersionMinorPutFromVersion(ctx, "eos", version, "source=agent");
28512962
snprintf(class, CF_MAXVARSIZE, "eos_%s", version);
28522963
EvalContextClassPutHard(ctx, class, "inventory,attribute_name=none,source=agent");
28532964
}
@@ -2872,6 +2983,7 @@ static int MiscOS(EvalContext *ctx)
28722983
EvalContextClassPutHard(ctx, "big_ip", "inventory,attribute_name=none,source=agent");
28732984
sscanf(buffer, "%*s %255s %*s %255s", version, build);
28742985
CanonifyNameInPlace(version);
2986+
SysOSVersionMinorPutFromVersion(ctx, "", version, "source=agent");
28752987
CanonifyNameInPlace(build);
28762988
snprintf(class, CF_MAXVARSIZE, "big_ip_%s", version);
28772989
EvalContextClassPutHard(ctx, class, "inventory,attribute_name=none,source=agent");
@@ -2907,6 +3019,7 @@ static int VM_Version(EvalContext *ctx)
29073019
snprintf(classbuf, CF_BUFSIZE, "VMware ESX Server %d.%d.%d", major, minor, bug);
29083020
EvalContextClassPutHard(ctx, classbuf, "inventory,attribute_name=none,source=agent");
29093021
sufficient = 1;
3022+
SysOSVersionMinorPutInt(ctx, minor, "source=agent");
29103023
}
29113024
else if (sscanf(buffer, "VMware ESX Server %255s", version) > 0)
29123025
{
@@ -3784,6 +3897,44 @@ static void SysOsVersionMajor(EvalContext *ctx)
37843897

37853898
/*****************************************************************************/
37863899

3900+
static void SysOSVersionMinorPutInt(EvalContext *ctx, int minor, char *tags)
3901+
{
3902+
char strminor[PRINTSIZE(minor)];
3903+
xsnprintf(strminor, sizeof(strminor), "%d", minor);
3904+
3905+
SysOSVersionMinorPut(ctx, strminor, tags);
3906+
}
3907+
3908+
static void SysOSVersionMinorPut(EvalContext *ctx, char *strminor, char *tags)
3909+
{
3910+
EvalContextVariablePutSpecial(ctx,
3911+
SPECIAL_SCOPE_SYS,
3912+
"os_version_minor",
3913+
strminor,
3914+
CF_DATA_TYPE_STRING,
3915+
tags
3916+
);
3917+
}
3918+
3919+
static void SysOsVersionMinorSetDefault(EvalContext *ctx)
3920+
{
3921+
DataType type_out;
3922+
const char *value = (const char *) EvalContextVariableGetSpecial(
3923+
ctx, SPECIAL_SCOPE_SYS, "os_version_minor", &type_out);
3924+
if (value == NULL)
3925+
{
3926+
EvalContextVariablePutSpecial(ctx,
3927+
SPECIAL_SCOPE_SYS,
3928+
"os_version_minor",
3929+
"Unknown",
3930+
CF_DATA_TYPE_STRING,
3931+
"source=agent"
3932+
);
3933+
}
3934+
}
3935+
3936+
/*****************************************************************************/
3937+
37873938
void DetectEnvironment(EvalContext *ctx)
37883939
{
37893940
GetNameInfo3(ctx);
@@ -3796,4 +3947,5 @@ void DetectEnvironment(EvalContext *ctx)
37963947
GetDefVars(ctx);
37973948
SysOSNameHuman(ctx);
37983949
SysOsVersionMajor(ctx);
3950+
SysOsVersionMinorSetDefault(ctx);
37993951
}

0 commit comments

Comments
 (0)