| 867 | } |
| 868 | |
| 869 | cbm_daemon_ipc_endpoint_t *cbm_daemon_ipc_endpoint_new(const char *instance_key, |
| 870 | const char *runtime_parent) { |
| 871 | if (!instance_key_valid(instance_key)) { |
| 872 | return NULL; |
| 873 | } |
| 874 | #ifdef __APPLE__ |
| 875 | const char *default_parent = "/private/tmp"; /* /tmp itself is a symlink on macOS. */ |
| 876 | #else |
| 877 | const char *default_parent = "/tmp"; |
| 878 | #endif |
| 879 | const char *requested_parent = runtime_parent ? runtime_parent : default_parent; |
| 880 | char canonical_parent[CBM_DAEMON_IPC_PATH_CAP]; |
| 881 | if (!cbm_canonical_path(requested_parent, canonical_parent, sizeof(canonical_parent))) { |
| 882 | return NULL; |
| 883 | } |
| 884 | const char *parent = canonical_parent; |
| 885 | if (!runtime_parent_valid(parent)) { |
| 886 | return NULL; |
| 887 | } |
| 888 | |
| 889 | cbm_daemon_ipc_endpoint_t *endpoint = calloc(1, sizeof(*endpoint)); |
| 890 | if (!endpoint) { |
| 891 | return NULL; |
| 892 | } |
| 893 | endpoint->dir_fd = -1; |
| 894 | endpoint->runtime_dir = |
| 895 | string_format("%s%s%s%lu", parent, parent[strlen(parent) - 1] == '/' ? "" : "/", |
| 896 | "cbm-daemon-", (unsigned long)geteuid()); |
| 897 | endpoint->socket_name = string_format("cbm-%s.sock", instance_key); |
| 898 | endpoint->socket_anchor_name = string_format("cbm-%s.anc", instance_key); |
| 899 | endpoint->socket_identity_name = string_format("cbm-%s.sock.identity", instance_key); |
| 900 | endpoint->socket_pending_name = string_format("cbm-%s.sock.pending", instance_key); |
| 901 | endpoint->lock_name = string_format("cbm-%s.lock", instance_key); |
| 902 | endpoint->startup_v2_lock_name = string_format("cbm-%s.startup-v2.lock", instance_key); |
| 903 | endpoint->lifetime_lock_name = string_format("cbm-%s.lifetime.lock", instance_key); |
| 904 | if (!endpoint->runtime_dir || !endpoint->socket_name || !endpoint->socket_anchor_name || |
| 905 | !endpoint->socket_identity_name || !endpoint->socket_pending_name || !endpoint->lock_name || |
| 906 | !endpoint->startup_v2_lock_name || !endpoint->lifetime_lock_name || |
| 907 | !private_runtime_open(endpoint->runtime_dir, &endpoint->dir_fd, &endpoint->dir_device, |
| 908 | &endpoint->dir_inode)) { |
| 909 | cbm_daemon_ipc_endpoint_free(endpoint); |
| 910 | return NULL; |
| 911 | } |
| 912 | endpoint->address = string_format("%s/%s", endpoint->runtime_dir, endpoint->socket_name); |
| 913 | endpoint->socket_anchor_address = |
| 914 | string_format("%s/%s", endpoint->runtime_dir, endpoint->socket_anchor_name); |
| 915 | struct sockaddr_un address; |
| 916 | socklen_t address_length; |
| 917 | if (!endpoint->address || !endpoint->socket_anchor_address || |
| 918 | strlen(endpoint->socket_anchor_name) >= POSIX_SOCKET_ANCHOR_NAME_CAP || |
| 919 | !unix_address_set(&address, endpoint->address, &address_length) || |
| 920 | !unix_address_set(&address, endpoint->socket_anchor_address, &address_length)) { |
| 921 | cbm_daemon_ipc_endpoint_free(endpoint); |
| 922 | return NULL; |
| 923 | } |
| 924 | return endpoint; |
| 925 | } |
| 926 | |