Преглед изворни кода

[settings] Add "unixtime" builtin setting to expose the current time

Expose the current wall-clock time (in seconds since the Epoch), since
this is often useful in captured boot logs and can also be useful when
checking unexpected X.509 certificate validation failures.

Use a :uint32 setting to avoid Y2K38 rollover, thereby ensuring that
this will eventually be somebody else's problem.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown пре 8 година
родитељ
комит
41f786cc0a
1 измењених фајлова са 33 додато и 0 уклоњено
  1. 33
    0
      src/core/settings.c

+ 33
- 0
src/core/settings.c Прегледај датотеку

@@ -31,6 +31,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31 31
 #include <byteswap.h>
32 32
 #include <errno.h>
33 33
 #include <assert.h>
34
+#include <time.h>
34 35
 #include <ipxe/in.h>
35 36
 #include <ipxe/ip.h>
36 37
 #include <ipxe/ipv6.h>
@@ -2552,6 +2553,38 @@ struct builtin_setting version_builtin_setting __builtin_setting = {
2552 2553
 	.fetch = version_fetch,
2553 2554
 };
2554 2555
 
2556
+/**
2557
+ * Fetch current time setting
2558
+ *
2559
+ * @v data		Buffer to fill with setting data
2560
+ * @v len		Length of buffer
2561
+ * @ret len		Length of setting data, or negative error
2562
+ */
2563
+static int unixtime_fetch ( void *data, size_t len ) {
2564
+	uint32_t content;
2565
+
2566
+	/* Return current time */
2567
+	content = htonl ( time(NULL) );
2568
+	if ( len > sizeof ( content ) )
2569
+		len = sizeof ( content );
2570
+	memcpy ( data, &content, len );
2571
+	return sizeof ( content );
2572
+}
2573
+
2574
+/** Current time setting */
2575
+const struct setting unixtime_setting __setting ( SETTING_MISC, unixtime ) = {
2576
+	.name = "unixtime",
2577
+	.description = "Seconds since the Epoch",
2578
+	.type = &setting_type_uint32,
2579
+	.scope = &builtin_scope,
2580
+};
2581
+
2582
+/** Current time built-in setting */
2583
+struct builtin_setting unixtime_builtin_setting __builtin_setting = {
2584
+	.setting = &unixtime_setting,
2585
+	.fetch = unixtime_fetch,
2586
+};
2587
+
2555 2588
 /**
2556 2589
  * Fetch built-in setting
2557 2590
  *

Loading…
Откажи
Сачувај