| 31 | } |
| 32 | |
| 33 | int unix_ss_create(const char *path, |
| 34 | const struct unix_stream_listen_opts *opts, |
| 35 | long timeout_ms, |
| 36 | struct unix_ss_socket **new_server_socket) |
| 37 | { |
| 38 | struct lock_file lock = LOCK_INIT; |
| 39 | int fd_socket; |
| 40 | struct unix_ss_socket *server_socket; |
| 41 | |
| 42 | *new_server_socket = NULL; |
| 43 | |
| 44 | if (timeout_ms < 0) |
| 45 | timeout_ms = DEFAULT_LOCK_TIMEOUT; |
| 46 | |
| 47 | /* |
| 48 | * Create a lock at "<path>.lock" if we can. |
| 49 | */ |
| 50 | if (hold_lock_file_for_update_timeout(&lock, path, 0, timeout_ms) < 0) |
| 51 | return -1; |
| 52 | |
| 53 | /* |
| 54 | * If another server is listening on "<path>" give up. We do not |
| 55 | * want to create a socket and steal future connections from them. |
| 56 | */ |
| 57 | if (is_another_server_alive(path, opts)) { |
| 58 | rollback_lock_file(&lock); |
| 59 | errno = EADDRINUSE; |
| 60 | return -2; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Create and bind to a Unix domain socket at "<path>". |
| 65 | */ |
| 66 | fd_socket = unix_stream_listen(path, opts); |
| 67 | if (fd_socket < 0) { |
| 68 | int saved_errno = errno; |
| 69 | rollback_lock_file(&lock); |
| 70 | errno = saved_errno; |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | server_socket = xcalloc(1, sizeof(*server_socket)); |
| 75 | server_socket->path_socket = strdup(path); |
| 76 | server_socket->fd_socket = fd_socket; |
| 77 | lstat(path, &server_socket->st_socket); |
| 78 | |
| 79 | *new_server_socket = server_socket; |
| 80 | |
| 81 | /* |
| 82 | * Always rollback (just delete) "<path>.lock" because we already created |
| 83 | * "<path>" as a socket and do not want to commit_lock to do the atomic |
| 84 | * rename trick. |
| 85 | */ |
| 86 | rollback_lock_file(&lock); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 |
no test coverage detected