+ err = symlink (lock_info_str, lfname) == 0 ? 0 : errno;
+ }
+
+ if (err == ENOSYS || err == LINKS_MIGHT_NOT_WORK || err == ENAMETOOLONG)
+ {
+ static char const nonce_base[] = ".#-emacsXXXXXX";
+ char *last_slash = strrchr (lfname, '/');
+ ptrdiff_t lfdirlen = last_slash + 1 - lfname;
+ USE_SAFE_ALLOCA;
+ char *nonce = SAFE_ALLOCA (lfdirlen + sizeof nonce_base);
+ int fd;
+ memcpy (nonce, lfname, lfdirlen);
+ strcpy (nonce + lfdirlen, nonce_base);
+
+ fd = mkostemp (nonce, O_BINARY | O_CLOEXEC);
+ if (fd < 0)
+ err = errno;
+ else
+ {
+ ptrdiff_t lock_info_len;
+ if (! O_CLOEXEC)
+ fcntl (fd, F_SETFD, FD_CLOEXEC);
+ lock_info_len = strlen (lock_info_str);
+ err = 0;
+ /* Use 'write', not 'emacs_write', as garbage collection
+ might signal an error, which would leak FD. */
+ if (write (fd, lock_info_str, lock_info_len) != lock_info_len
+ || fchmod (fd, S_IRUSR | S_IRGRP | S_IROTH) != 0)
+ err = errno;
+ /* There is no need to call fsync here, as the contents of
+ the lock file need not survive system crashes. */
+ if (emacs_close (fd) != 0)
+ err = errno;
+ if (!err && rename_lock_file (nonce, lfname, force) != 0)
+ err = errno;
+ if (err)
+ unlink (nonce);
+ }
+
+ SAFE_FREE ();