+int pa_client_conf_load_cookie(pa_client_conf *c, uint8_t *cookie, size_t cookie_length) {
+ int r;
+
+ pa_assert(c);
+ pa_assert(cookie);
+ pa_assert(cookie_length > 0);
+
+ if (c->cookie_file_from_env) {
+ r = pa_authkey_load_auto(c->cookie_file_from_env, true, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
+
+ pa_log_warn("Failed to load cookie from %s (configured with environment variable PULSE_COOKIE): %s",
+ c->cookie_file_from_env, pa_cstrerror(errno));
+ }
+
+ if (c->cookie_from_x11_valid) {
+ if (cookie_length == sizeof(c->cookie_from_x11)) {
+ memcpy(cookie, c->cookie_from_x11, cookie_length);
+ return 0;
+ }
+
+ pa_log_warn("Failed to load cookie from X11 root window property PULSE_COOKIE: size mismatch.");
+ }
+
+ if (c->cookie_file_from_application) {
+ r = pa_authkey_load_auto(c->cookie_file_from_application, true, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
+
+ pa_log_warn("Failed to load cookie from %s (configured by the application): %s", c->cookie_file_from_application,
+ pa_cstrerror(errno));
+ }
+
+ if (c->cookie_file_from_client_conf) {
+ r = pa_authkey_load_auto(c->cookie_file_from_client_conf, true, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
+
+ pa_log_warn("Failed to load cookie from %s (configured in client.conf): %s", c->cookie_file_from_client_conf,
+ pa_cstrerror(errno));
+ }
+
+ r = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE, false, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
+
+ r = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE_FALLBACK, false, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
+
+ r = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE, true, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
+
+ pa_log("Failed to load cookie file from %s: %s", PA_NATIVE_COOKIE_FILE, pa_cstrerror(errno));
+ memset(cookie, 0, cookie_length);
+
+ return -1;
+}
+