+
+ if (random_fd >= 0)
+ close(random_fd);
+
+ return ret;
+}
+
+static int load(const char *fn, void *data, size_t length) {
+ int fd = -1;
+ int writable = 1;
+ int unlock = 0, ret;
+ ssize_t r;
+ assert(fn && data && length);
+
+ if ((fd = open(fn, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
+ if (errno != EACCES || (fd = open(fn, O_RDONLY)) < 0) {
+ pa_log(__FILE__": failed to open cookie file '%s': %s\n", fn, strerror(errno));
+ goto finish;
+ } else
+ writable = 0;
+ }
+
+ unlock = pa_lock_fd(fd, 1) >= 0;
+
+ if ((r = pa_loop_read(fd, data, length)) < 0) {
+ pa_log(__FILE__": failed to read cookie file '%s': %s\n", fn, strerror(errno));
+ goto finish;
+ }
+
+ if ((size_t) r != length) {
+
+ if (!writable) {
+ pa_log(__FILE__": unable to write cookie to read only file\n");
+ goto finish;
+ }
+
+ if (generate(fd, data, length) < 0)
+ goto finish;
+ }
+
+ ret = 0;
+
+finish:
+