#include <errno.h>
#include <string.h>
#include <unistd.h>
+#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/time.h>
#include <fcntl.h>
#include "libubi.h"
#include "common.h"
close(fd);
return -1;
}
+
+/**
+ * seed_random_generator - randomly seed the standard pseudo-random generator.
+ *
+ * This helper function seeds the standard libc pseudo-random generator with a
+ * more or less random value to make sure the 'rand()' call does not return the
+ * same sequence every time UBI utilities run. Returns the random seed in case
+ * of success and a %-1 in case of error.
+ */
+int seed_random_generator(void)
+{
+ struct timeval tv;
+ struct timezone tz;
+ int seed;
+
+ /*
+ * Just assume that a combination of the PID + current time is a
+ * reasonably random number.
+ */
+ if (gettimeofday(&tv, &tz))
+ return -1;
+
+ seed = (unsigned int)tv.tv_sec;
+ seed += (unsigned int)tv.tv_usec;
+ seed *= getpid();
+ seed %= INT_MAX;
+ srand(seed);
+ return seed;
+}
(s)/5-3, (s)/5-2, (s)/5-1, (s)/5+1, (s)/5+2, (s)/5+3, (s)-17, (s)-9, \
(s)-8, (s)-6, (s)-4, (s)-1, (s)};
+extern int seed_random_generator(void);
+
extern void __errmsg(const char *test, const char *func, int line,
const char *fmt, ...);
-void __failed(const char *test, const char *func, int line,
- const char *failed);
-int __initial_check(const char *test, int argc, char * const argv[]);
-int __check_volume(libubi_t libubi, struct ubi_dev_info *dev_info,
- const char *test, const char *func, int line, int vol_id,
- const struct ubi_mkvol_request *req);
-int __check_vol_patt(libubi_t libubi, const char *test, const char *func,
- int line, const char *node, uint8_t byte);
-int __update_vol_patt(libubi_t libubi, const char *test, const char *func,
- int line, const char *node, long long bytes,
- uint8_t byte);
+extern void __failed(const char *test, const char *func, int line,
+ const char *failed);
+extern int __initial_check(const char *test, int argc, char * const argv[]);
+extern int __check_volume(libubi_t libubi, struct ubi_dev_info *dev_info,
+ const char *test, const char *func, int line,
+ int vol_id, const struct ubi_mkvol_request *req);
+extern int __check_vol_patt(libubi_t libubi, const char *test, const char *func,
+ int line, const char *node, uint8_t byte);
+extern int __update_vol_patt(libubi_t libubi, const char *test, const char *func,
+ int line, const char *node, long long bytes,
+ uint8_t byte);
#ifdef __cplusplus
}