@@ -2043,13 +2043,39 @@ static int machine_id_read_and_sanitize(char *path,
20432043 return 0 ;
20442044}
20452045
2046+ int write_uuid_to_file (char * filename , char * uuid ) {
2047+ int fd ;
2048+ size_t uuid_len ;
2049+
2050+ if (filename == NULL || uuid == NULL ) {
2051+ return FLB_FALSE ;
2052+ }
2053+
2054+ /* write uuid to file */
2055+ fd = flb_open (filename , O_CREAT | O_WRONLY | O_TRUNC , 0666 );
2056+ if (fd == -1 ) {
2057+ return FLB_FALSE ;
2058+ }
2059+
2060+ uuid_len = strlen (uuid );
2061+
2062+ if (flb_write (fd , uuid , uuid_len ) != uuid_len ) {
2063+ flb_close (fd );
2064+ return FLB_FALSE ;
2065+ }
2066+
2067+ flb_close (fd );
2068+ return FLB_TRUE ;
2069+ }
2070+
20462071int flb_utils_get_machine_id (char * * out_id , size_t * out_size )
20472072{
20482073 int ret ;
20492074 char * id ;
20502075 size_t bytes ;
20512076 char * uuid ;
20522077 int fallback = FLB_FALSE ;
2078+ char * fallback_id = "machine-id" ; //should reside in current working directory
20532079
20542080#ifdef __linux__
20552081 char * dbus_var = "/var/lib/dbus/machine-id" ;
@@ -2084,6 +2110,24 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
20842110 return 0 ;
20852111 }
20862112 }
2113+
2114+ if (access (fallback_id , F_OK ) == 0 )
2115+ {
2116+ ret = machine_id_read_and_sanitize (fallback_id , & id , & bytes );
2117+ if (ret == 0 )
2118+ {
2119+ if (bytes == 0 ) {
2120+ /* guid is somewhat corrupted */
2121+ fallback = FLB_TRUE ;
2122+ goto fallback ;
2123+ }
2124+ }
2125+
2126+ * out_id = id ;
2127+ * out_size = bytes ;
2128+ return 0 ;
2129+ }
2130+
20872131#elif defined(__FreeBSD__ ) || defined(__NetBSD__ ) || \
20882132 defined(__OpenBSD__ ) || defined(__DragonFly__ )
20892133
@@ -2175,7 +2219,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
21752219#endif
21762220
21772221fallback :
2178-
2222+
21792223 flb_warn ("falling back on random machine UUID" );
21802224
21812225 /* generate a random uuid */
@@ -2185,15 +2229,22 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
21852229 return -1 ;
21862230 }
21872231 ret = flb_utils_uuid_v4_gen (uuid );
2232+
21882233 if (ret == 0 ) {
2234+
2235+ int write_result = write_uuid_to_file (fallback_id , uuid );
2236+ if (write_result != 0 )
2237+ {
2238+ //writing failed, next uuid generation
2239+ flb_warn ("failed to write machine-id to file %s" , fallback_id );
2240+ }
21892241 * out_id = uuid ;
21902242 * out_size = strlen (uuid );
21912243 if (fallback == FLB_TRUE ) {
21922244 return 2 ;
21932245 }
21942246 return 0 ;
21952247 }
2196-
21972248 return -1 ;
21982249}
21992250
0 commit comments