| 889 | } |
| 890 | |
| 891 | static struct memif_socket * |
| 892 | memif_socket_create(char *key, uint8_t listener, bool is_abstract, uid_t owner_uid, gid_t owner_gid) |
| 893 | { |
| 894 | struct memif_socket *sock; |
| 895 | struct sockaddr_un un = { 0 }; |
| 896 | uint32_t sunlen; |
| 897 | int sockfd; |
| 898 | int ret; |
| 899 | int on = 1; |
| 900 | |
| 901 | sock = rte_zmalloc("memif-socket", sizeof(struct memif_socket), 0); |
| 902 | if (sock == NULL) { |
| 903 | MIF_LOG(ERR, "Failed to allocate memory for memif socket"); |
| 904 | return NULL; |
| 905 | } |
| 906 | |
| 907 | sock->listener = listener; |
| 908 | strlcpy(sock->filename, key, MEMIF_SOCKET_UN_SIZE); |
| 909 | TAILQ_INIT(&sock->dev_queue); |
| 910 | |
| 911 | if (listener != 0) { |
| 912 | sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 913 | if (sockfd < 0) |
| 914 | goto error; |
| 915 | |
| 916 | un.sun_family = AF_UNIX; |
| 917 | if (is_abstract) { |
| 918 | /* abstract address */ |
| 919 | un.sun_path[0] = '\0'; |
| 920 | strlcpy(un.sun_path + 1, sock->filename, MEMIF_SOCKET_UN_SIZE - 1); |
| 921 | sunlen = RTE_MIN(1 + strlen(sock->filename), |
| 922 | MEMIF_SOCKET_UN_SIZE) + |
| 923 | sizeof(un) - sizeof(un.sun_path); |
| 924 | } else { |
| 925 | sunlen = sizeof(un); |
| 926 | strlcpy(un.sun_path, sock->filename, MEMIF_SOCKET_UN_SIZE); |
| 927 | } |
| 928 | |
| 929 | ret = setsockopt(sockfd, SOL_SOCKET, SO_PASSCRED, &on, |
| 930 | sizeof(on)); |
| 931 | if (ret < 0) |
| 932 | goto error; |
| 933 | |
| 934 | ret = bind(sockfd, (struct sockaddr *)&un, sunlen); |
| 935 | if (ret < 0) |
| 936 | goto error; |
| 937 | |
| 938 | ret = listen(sockfd, 1); |
| 939 | if (ret < 0) |
| 940 | goto error; |
| 941 | |
| 942 | MIF_LOG(DEBUG, "Memif listener socket %s created.", sock->filename); |
| 943 | |
| 944 | if (!is_abstract && (owner_uid != (uid_t)-1 || owner_gid != (gid_t)-1)) { |
| 945 | ret = chown(sock->filename, owner_uid, owner_gid); |
| 946 | if (ret < 0) { |
| 947 | MIF_LOG(ERR, "Failed to change listener socket owner"); |
| 948 | goto error; |
no test coverage detected