diff options
Diffstat (limited to 'kms/server/kms_server.c')
-rw-r--r-- | kms/server/kms_server.c | 422 |
1 files changed, 282 insertions, 140 deletions
diff --git a/kms/server/kms_server.c b/kms/server/kms_server.c index 5aa6590..2eaa1ed 100644 --- a/kms/server/kms_server.c +++ b/kms/server/kms_server.c @@ -13,20 +13,19 @@ #include <xf86drm.h> #include <xf86drmMode.h> -#include <libdrm/drm_mode.h> +#include <drm_mode.h> #define MAX_CONNECTORS 32 typedef struct { int drmfd; - uint32_t plane_ids[GSR_KMS_MAX_PLANES]; - uint32_t connector_ids[GSR_KMS_MAX_PLANES]; - size_t num_plane_ids; + drmModePlaneResPtr planes; } gsr_drm; typedef struct { uint32_t connector_id; uint64_t crtc_id; + uint64_t hdr_metadata_blob_id; } connector_crtc_pair; typedef struct { @@ -70,6 +69,40 @@ static int send_msg_to_client(int client_fd, gsr_kms_response *response) { return sendmsg(client_fd, &response_message, 0); } +static int recv_msg_from_client(int client_fd, gsr_kms_request *request) { + struct iovec iov; + iov.iov_base = request; + iov.iov_len = sizeof(*request); + + struct msghdr response_message = {0}; + response_message.msg_iov = &iov; + response_message.msg_iovlen = 1; + + char cmsgbuf[CMSG_SPACE(sizeof(int) * 1)]; + memset(cmsgbuf, 0, sizeof(cmsgbuf)); + response_message.msg_control = cmsgbuf; + response_message.msg_controllen = sizeof(cmsgbuf); + + int res = recvmsg(client_fd, &response_message, MSG_WAITALL); + if(res <= 0) + return res; + + if(request->new_connection_fd > 0) { + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message); + if(cmsg) { + int *fds = (int*)CMSG_DATA(cmsg); + request->new_connection_fd = fds[0]; + } else { + if(request->new_connection_fd > 0) { + close(request->new_connection_fd); + request->new_connection_fd = 0; + } + } + } + + return res; +} + static bool connector_get_property_by_name(int drmfd, drmModeConnectorPtr props, const char *name, uint64_t *result) { for(int i = 0; i < props->count_props; ++i) { drmModePropertyPtr prop = drmModeGetProperty(drmfd, props->props[i]); @@ -85,149 +118,168 @@ static bool connector_get_property_by_name(int drmfd, drmModeConnectorPtr props, return false; } -static bool plane_is_cursor_plane(int drmfd, uint32_t plane_id) { +typedef enum { + PLANE_PROPERTY_X = 1 << 0, + PLANE_PROPERTY_Y = 1 << 1, + PLANE_PROPERTY_SRC_X = 1 << 2, + PLANE_PROPERTY_SRC_Y = 1 << 3, + PLANE_PROPERTY_SRC_W = 1 << 4, + PLANE_PROPERTY_SRC_H = 1 << 5, + PLANE_PROPERTY_IS_CURSOR = 1 << 6, + PLANE_PROPERTY_IS_PRIMARY = 1 << 7, +} plane_property_mask; + +/* Returns plane_property_mask */ +static uint32_t plane_get_properties(int drmfd, uint32_t plane_id, int *x, int *y, int *src_x, int *src_y, int *src_w, int *src_h) { + *x = 0; + *y = 0; + *src_x = 0; + *src_y = 0; + *src_w = 0; + *src_h = 0; + + plane_property_mask property_mask = 0; + drmModeObjectPropertiesPtr props = drmModeObjectGetProperties(drmfd, plane_id, DRM_MODE_OBJECT_PLANE); if(!props) - return false; + return property_mask; + // TODO: Dont do this every frame for(uint32_t i = 0; i < props->count_props; ++i) { drmModePropertyPtr prop = drmModeGetProperty(drmfd, props->props[i]); - if(prop) { - if(strcmp(prop->name, "type") == 0) { - const uint64_t current_enum_value = props->prop_values[i]; - bool is_cursor = false; - - for(int j = 0; j < prop->count_enums; ++j) { - if(prop->enums[j].value == current_enum_value && strcmp(prop->enums[j].name, "Cursor") == 0) { - is_cursor = true; - break; - } + if(!prop) + continue; + // SRC_* values are fixed 16.16 points + const uint32_t type = prop->flags & (DRM_MODE_PROP_LEGACY_TYPE | DRM_MODE_PROP_EXTENDED_TYPE); + if((type & DRM_MODE_PROP_SIGNED_RANGE) && strcmp(prop->name, "CRTC_X") == 0) { + *x = (int)props->prop_values[i]; + property_mask |= PLANE_PROPERTY_X; + } else if((type & DRM_MODE_PROP_SIGNED_RANGE) && strcmp(prop->name, "CRTC_Y") == 0) { + *y = (int)props->prop_values[i]; + property_mask |= PLANE_PROPERTY_Y; + } else if((type & DRM_MODE_PROP_RANGE) && strcmp(prop->name, "SRC_X") == 0) { + *src_x = (int)(props->prop_values[i] >> 16); + property_mask |= PLANE_PROPERTY_SRC_X; + } else if((type & DRM_MODE_PROP_RANGE) && strcmp(prop->name, "SRC_Y") == 0) { + *src_y = (int)(props->prop_values[i] >> 16); + property_mask |= PLANE_PROPERTY_SRC_Y; + } else if((type & DRM_MODE_PROP_RANGE) && strcmp(prop->name, "SRC_W") == 0) { + *src_w = (int)(props->prop_values[i] >> 16); + property_mask |= PLANE_PROPERTY_SRC_W; + } else if((type & DRM_MODE_PROP_RANGE) && strcmp(prop->name, "SRC_H") == 0) { + *src_h = (int)(props->prop_values[i] >> 16); + property_mask |= PLANE_PROPERTY_SRC_H; + } else if((type & DRM_MODE_PROP_ENUM) && strcmp(prop->name, "type") == 0) { + const uint64_t current_enum_value = props->prop_values[i]; + for(int j = 0; j < prop->count_enums; ++j) { + if(prop->enums[j].value == current_enum_value && strcmp(prop->enums[j].name, "Primary") == 0) { + property_mask |= PLANE_PROPERTY_IS_PRIMARY; + break; + } else if(prop->enums[j].value == current_enum_value && strcmp(prop->enums[j].name, "Cursor") == 0) { + property_mask |= PLANE_PROPERTY_IS_CURSOR; + break; } - - drmModeFreeProperty(prop); - return is_cursor; } - drmModeFreeProperty(prop); } + + drmModeFreeProperty(prop); } drmModeFreeObjectProperties(props); - return false; + return property_mask; } /* Returns 0 if not found */ -static uint32_t get_connector_by_crtc_id(const connector_to_crtc_map *c2crtc_map, uint32_t crtc_id) { +static const connector_crtc_pair* get_connector_pair_by_crtc_id(const connector_to_crtc_map *c2crtc_map, uint32_t crtc_id) { for(int i = 0; i < c2crtc_map->num_maps; ++i) { if(c2crtc_map->maps[i].crtc_id == crtc_id) - return c2crtc_map->maps[i].connector_id; + return &c2crtc_map->maps[i]; } - return 0; + return NULL; } -static int kms_get_plane_ids(gsr_drm *drm) { - drmModePlaneResPtr planes = NULL; - drmModeResPtr resources = NULL; - int result = -1; - - connector_to_crtc_map c2crtc_map; - c2crtc_map.num_maps = 0; - - if(drmSetClientCap(drm->drmfd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) != 0) { - fprintf(stderr, "kms server error: drmSetClientCap DRM_CLIENT_CAP_UNIVERSAL_PLANES failed, error: %s\n", strerror(errno)); - goto error; - } +static void map_crtc_to_connector_ids(gsr_drm *drm, connector_to_crtc_map *c2crtc_map) { + c2crtc_map->num_maps = 0; + drmModeResPtr resources = drmModeGetResources(drm->drmfd); + if(!resources) + return; - if(drmSetClientCap(drm->drmfd, DRM_CLIENT_CAP_ATOMIC, 1) != 0) { - fprintf(stderr, "kms server warning: drmSetClientCap DRM_CLIENT_CAP_ATOMIC failed, error: %s. The wrong monitor may be captured as a result\n", strerror(errno)); - } + for(int i = 0; i < resources->count_connectors && c2crtc_map->num_maps < MAX_CONNECTORS; ++i) { + drmModeConnectorPtr connector = drmModeGetConnectorCurrent(drm->drmfd, resources->connectors[i]); + if(!connector) + continue; - planes = drmModeGetPlaneResources(drm->drmfd); - if(!planes) { - fprintf(stderr, "kms server error: failed to access planes, error: %s\n", strerror(errno)); - goto error; - } + uint64_t crtc_id = 0; + connector_get_property_by_name(drm->drmfd, connector, "CRTC_ID", &crtc_id); - resources = drmModeGetResources(drm->drmfd); - if(resources) { - for(int i = 0; i < resources->count_connectors && c2crtc_map.num_maps < MAX_CONNECTORS; ++i) { - drmModeConnectorPtr connector = drmModeGetConnectorCurrent(drm->drmfd, resources->connectors[i]); - if(connector) { - uint64_t crtc_id = 0; - connector_get_property_by_name(drm->drmfd, connector, "CRTC_ID", &crtc_id); + uint64_t hdr_output_metadata_blob_id = 0; + connector_get_property_by_name(drm->drmfd, connector, "HDR_OUTPUT_METADATA", &hdr_output_metadata_blob_id); - c2crtc_map.maps[c2crtc_map.num_maps].connector_id = connector->connector_id; - c2crtc_map.maps[c2crtc_map.num_maps].crtc_id = crtc_id; - ++c2crtc_map.num_maps; + c2crtc_map->maps[c2crtc_map->num_maps].connector_id = connector->connector_id; + c2crtc_map->maps[c2crtc_map->num_maps].crtc_id = crtc_id; + c2crtc_map->maps[c2crtc_map->num_maps].hdr_metadata_blob_id = hdr_output_metadata_blob_id; + ++c2crtc_map->num_maps; - drmModeFreeConnector(connector); - } - } - drmModeFreeResources(resources); + drmModeFreeConnector(connector); } + drmModeFreeResources(resources); +} - for(uint32_t i = 0; i < planes->count_planes && drm->num_plane_ids < GSR_KMS_MAX_PLANES; ++i) { - drmModePlanePtr plane = drmModeGetPlane(drm->drmfd, planes->planes[i]); - if(!plane) { - fprintf(stderr, "kms server warning: failed to get drmModePlanePtr for plane %#x: %s (%d)\n", planes->planes[i], strerror(errno), errno); +static void drm_mode_cleanup_handles(int drmfd, drmModeFB2Ptr drmfb) { + for(int i = 0; i < 4; ++i) { + if(!drmfb->handles[i]) continue; - } - if(!plane->fb_id) { - drmModeFreePlane(plane); - continue; + bool already_closed = false; + for(int j = 0; j < i; ++j) { + if(drmfb->handles[i] == drmfb->handles[j]) { + already_closed = true; + break; + } } - if(plane_is_cursor_plane(drm->drmfd, plane->plane_id)) + if(already_closed) continue; - // TODO: Fallback to getfb(1)? - drmModeFB2Ptr drmfb = drmModeGetFB2(drm->drmfd, plane->fb_id); - if(drmfb) { - drm->plane_ids[drm->num_plane_ids] = plane->plane_id; - drm->connector_ids[drm->num_plane_ids] = get_connector_by_crtc_id(&c2crtc_map, plane->crtc_id); - ++drm->num_plane_ids; - drmModeFreeFB2(drmfb); - } - drmModeFreePlane(plane); + drmCloseBufferHandle(drmfd, drmfb->handles[i]); } +} - result = 0; - - error: - if(planes) - drmModeFreePlaneResources(planes); +static bool get_hdr_metadata(int drm_fd, uint64_t hdr_metadata_blob_id, struct hdr_output_metadata *hdr_metadata) { + drmModePropertyBlobPtr hdr_metadata_blob = drmModeGetPropertyBlob(drm_fd, hdr_metadata_blob_id); + if(!hdr_metadata_blob) + return false; - return result; -} + if(hdr_metadata_blob->length >= sizeof(struct hdr_output_metadata)) + *hdr_metadata = *(struct hdr_output_metadata*)hdr_metadata_blob->data; -static bool drmfb_has_multiple_handles(drmModeFB2 *drmfb) { - int num_handles = 0; - for(uint32_t handle_index = 0; handle_index < 4 && drmfb->handles[handle_index]; ++handle_index) { - ++num_handles; - } - return num_handles > 1; + drmModeFreePropertyBlob(hdr_metadata_blob); + return true; } -static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response) { +static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response, connector_to_crtc_map *c2crtc_map) { int result = -1; response->result = KMS_RESULT_OK; response->err_msg[0] = '\0'; response->num_fds = 0; - for(size_t i = 0; i < drm->num_plane_ids && response->num_fds < GSR_KMS_MAX_PLANES; ++i) { + for(uint32_t i = 0; i < drm->planes->count_planes && response->num_fds < GSR_KMS_MAX_PLANES; ++i) { drmModePlanePtr plane = NULL; - drmModeFB2 *drmfb = NULL; + drmModeFB2Ptr drmfb = NULL; - plane = drmModeGetPlane(drm->drmfd, drm->plane_ids[i]); + plane = drmModeGetPlane(drm->drmfd, drm->planes->planes[i]); if(!plane) { response->result = KMS_RESULT_FAILED_TO_GET_PLANE; - snprintf(response->err_msg, sizeof(response->err_msg), "failed to get drm plane with id %u, error: %s\n", drm->plane_ids[i], strerror(errno)); + snprintf(response->err_msg, sizeof(response->err_msg), "failed to get drm plane with id %u, error: %s\n", drm->planes->planes[i], strerror(errno)); fprintf(stderr, "kms server error: %s\n", response->err_msg); goto next; } + if(!plane->fb_id) + goto next; + drmfb = drmModeGetFB2(drm->drmfd, plane->fb_id); if(!drmfb) { // Commented out for now because we get here if the cursor is moved to another monitor and we dont care about the cursor @@ -241,7 +293,7 @@ static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response) { response->result = KMS_RESULT_FAILED_TO_GET_PLANE; snprintf(response->err_msg, sizeof(response->err_msg), "drmfb handle is NULL"); fprintf(stderr, "kms server error: %s\n", response->err_msg); - goto next; + goto cleanup_handles; } // TODO: Check if dimensions have changed by comparing width and height to previous time this was called. @@ -253,19 +305,49 @@ static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response) { response->result = KMS_RESULT_FAILED_TO_GET_PLANE; snprintf(response->err_msg, sizeof(response->err_msg), "failed to get fd from drm handle, error: %s", strerror(errno)); fprintf(stderr, "kms server error: %s\n", response->err_msg); - continue; + goto cleanup_handles; + } + + const int fd_index = response->num_fds; + + int x = 0, y = 0, src_x = 0, src_y = 0, src_w = 0, src_h = 0; + plane_property_mask property_mask = plane_get_properties(drm->drmfd, plane->plane_id, &x, &y, &src_x, &src_y, &src_w, &src_h); + if((property_mask & PLANE_PROPERTY_IS_PRIMARY) || (property_mask & PLANE_PROPERTY_IS_CURSOR)) { + const connector_crtc_pair *crtc_pair = get_connector_pair_by_crtc_id(c2crtc_map, plane->crtc_id); + if(crtc_pair && crtc_pair->hdr_metadata_blob_id) { + response->fds[fd_index].has_hdr_metadata = get_hdr_metadata(drm->drmfd, crtc_pair->hdr_metadata_blob_id, &response->fds[fd_index].hdr_metadata); + } else { + response->fds[fd_index].has_hdr_metadata = false; + } + + response->fds[fd_index].fd = fb_fd; + response->fds[fd_index].width = drmfb->width; + response->fds[fd_index].height = drmfb->height; + response->fds[fd_index].pitch = drmfb->pitches[0]; + response->fds[fd_index].offset = drmfb->offsets[0]; + response->fds[fd_index].pixel_format = drmfb->pixel_format; + response->fds[fd_index].modifier = drmfb->modifier; + response->fds[fd_index].connector_id = crtc_pair ? crtc_pair->connector_id : 0; + response->fds[fd_index].is_cursor = property_mask & PLANE_PROPERTY_IS_CURSOR; + response->fds[fd_index].is_combined_plane = false; + if(property_mask & PLANE_PROPERTY_IS_CURSOR) { + response->fds[fd_index].x = x; + response->fds[fd_index].y = y; + response->fds[fd_index].src_w = 0; + response->fds[fd_index].src_h = 0; + } else { + response->fds[fd_index].x = src_x; + response->fds[fd_index].y = src_y; + response->fds[fd_index].src_w = src_w; + response->fds[fd_index].src_h = src_h; + } + ++response->num_fds; + } else { + close(fb_fd); } - response->fds[response->num_fds].fd = fb_fd; - response->fds[response->num_fds].width = drmfb->width; - response->fds[response->num_fds].height = drmfb->height; - response->fds[response->num_fds].pitch = drmfb->pitches[0]; - response->fds[response->num_fds].offset = drmfb->offsets[0]; - response->fds[response->num_fds].pixel_format = drmfb->pixel_format; - response->fds[response->num_fds].modifier = drmfb->modifier; - response->fds[response->num_fds].connector_id = drm->connector_ids[i]; - response->fds[response->num_fds].is_combined_plane = drmfb_has_multiple_handles(drmfb); - ++response->num_fds; + cleanup_handles: + drm_mode_cleanup_handles(drm->drmfd, drmfb); next: if(drmfb) @@ -274,7 +356,10 @@ static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response) { drmModeFreePlane(plane); } - if(response->num_fds > 0 || response->result == KMS_RESULT_OK) { + if(response->num_fds > 0) + response->result = KMS_RESULT_OK; + + if(response->result == KMS_RESULT_OK) { result = 0; } else { for(int i = 0; i < response->num_fds; ++i) { @@ -294,7 +379,7 @@ static double clock_get_monotonic_seconds(void) { return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001; } -static void strncpy_safe(char *dst, const char *src, int len) { +static void string_copy(char *dst, const char *src, int len) { int src_len = strlen(src); int min_len = src_len; if(len - 1 < min_len) @@ -304,13 +389,19 @@ static void strncpy_safe(char *dst, const char *src, int len) { } int main(int argc, char **argv) { + int res = 0; + int socket_fd = 0; + gsr_drm drm; + drm.drmfd = 0; + drm.planes = NULL; + if(argc != 3) { - fprintf(stderr, "usage: kms_server <domain_socket_path> <card_path>\n"); + fprintf(stderr, "usage: gsr-kms-server <domain_socket_path> <card_path>\n"); return 1; } const char *domain_socket_path = argv[1]; - int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0); + socket_fd = socket(AF_UNIX, SOCK_STREAM, 0); if(socket_fd == -1) { fprintf(stderr, "kms server error: failed to create socket, error: %s\n", strerror(errno)); return 2; @@ -318,19 +409,34 @@ int main(int argc, char **argv) { const char *card_path = argv[2]; - gsr_drm drm; - drm.num_plane_ids = 0; drm.drmfd = open(card_path, O_RDONLY); if(drm.drmfd < 0) { fprintf(stderr, "kms server error: failed to open %s, error: %s", card_path, strerror(errno)); - return 2; + res = 2; + goto done; } - if(kms_get_plane_ids(&drm) != 0) { - close(drm.drmfd); - return 2; + if(drmSetClientCap(drm.drmfd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) != 0) { + fprintf(stderr, "kms server error: drmSetClientCap DRM_CLIENT_CAP_UNIVERSAL_PLANES failed, error: %s\n", strerror(errno)); + res = 2; + goto done; } + if(drmSetClientCap(drm.drmfd, DRM_CLIENT_CAP_ATOMIC, 1) != 0) { + fprintf(stderr, "kms server warning: drmSetClientCap DRM_CLIENT_CAP_ATOMIC failed, error: %s. The wrong monitor may be captured as a result\n", strerror(errno)); + } + + drm.planes = drmModeGetPlaneResources(drm.drmfd); + if(!drm.planes) { + fprintf(stderr, "kms server error: failed to get plane resources, error: %s\n", strerror(errno)); + res = 2; + goto done; + } + + connector_to_crtc_map c2crtc_map; + c2crtc_map.num_maps = 0; + map_crtc_to_connector_ids(&drm, &c2crtc_map); + fprintf(stderr, "kms server info: connecting to the client\n"); bool connected = false; const double connect_timeout_sec = 5.0; @@ -338,7 +444,7 @@ int main(int argc, char **argv) { while(clock_get_monotonic_seconds() - start_time < connect_timeout_sec) { struct sockaddr_un remote_addr = {0}; remote_addr.sun_family = AF_UNIX; - strncpy_safe(remote_addr.sun_path, domain_socket_path, sizeof(remote_addr.sun_path)); + string_copy(remote_addr.sun_path, domain_socket_path, sizeof(remote_addr.sun_path)); // TODO: Check if parent disconnected if(connect(socket_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr.sun_family) + strlen(remote_addr.sun_path)) == -1) { if(errno == ECONNREFUSED || errno == ENOENT) { @@ -349,8 +455,8 @@ int main(int argc, char **argv) { } fprintf(stderr, "kms server error: connect failed, error: %s (%d)\n", strerror(errno), errno); - close(drm.drmfd); - return 2; + res = 2; + goto done; } next: @@ -361,21 +467,17 @@ int main(int argc, char **argv) { fprintf(stderr, "kms server info: connected to the client\n"); } else { fprintf(stderr, "kms server error: failed to connect to the client in %f seconds\n", connect_timeout_sec); - close(drm.drmfd); - return 2; + res = 2; + goto done; } - int res = 0; for(;;) { gsr_kms_request request; - struct iovec iov; - iov.iov_base = &request; - iov.iov_len = sizeof(request); - - struct msghdr request_message = {0}; - request_message.msg_iov = &iov; - request_message.msg_iovlen = 1; - const int recv_res = recvmsg(socket_fd, &request_message, MSG_WAITALL); + request.version = 0; + request.type = -1; + request.new_connection_fd = 0; + + const int recv_res = recv_msg_from_client(socket_fd, &request); if(recv_res == 0) { fprintf(stderr, "kms server info: kms client shutdown, shutting down the server\n"); res = 3; @@ -391,40 +493,80 @@ int main(int argc, char **argv) { continue; } + if(request.version != GSR_KMS_PROTOCOL_VERSION) { + fprintf(stderr, "kms server error: expected gpu screen recorder protocol version to be %u, but it's %u\n", GSR_KMS_PROTOCOL_VERSION, request.version); + /* + if(request.new_connection_fd > 0) + close(request.new_connection_fd); + */ + continue; + } + switch(request.type) { + case KMS_REQUEST_TYPE_REPLACE_CONNECTION: { + gsr_kms_response response; + response.version = GSR_KMS_PROTOCOL_VERSION; + response.num_fds = 0; + + if(request.new_connection_fd > 0) { + if(socket_fd > 0) + close(socket_fd); + socket_fd = request.new_connection_fd; + + response.result = KMS_RESULT_OK; + if(send_msg_to_client(socket_fd, &response) == -1) + fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_REPLACE_CONNECTION request\n"); + } else { + response.result = KMS_RESULT_INVALID_REQUEST; + snprintf(response.err_msg, sizeof(response.err_msg), "received invalid connection fd"); + fprintf(stderr, "kms server error: %s\n", response.err_msg); + if(send_msg_to_client(socket_fd, &response) == -1) + fprintf(stderr, "kms server error: failed to respond to client request\n"); + } + + break; + } case KMS_REQUEST_TYPE_GET_KMS: { gsr_kms_response response; + response.version = GSR_KMS_PROTOCOL_VERSION; + response.num_fds = 0; - if(kms_get_fb(&drm, &response) == 0) { + if(kms_get_fb(&drm, &response, &c2crtc_map) == 0) { if(send_msg_to_client(socket_fd, &response) == -1) fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n"); - - for(int i = 0; i < response.num_fds; ++i) { - close(response.fds[i].fd); - } } else { if(send_msg_to_client(socket_fd, &response) == -1) fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n"); } + for(int i = 0; i < response.num_fds; ++i) { + close(response.fds[i].fd); + } + break; } default: { gsr_kms_response response; + response.version = GSR_KMS_PROTOCOL_VERSION; response.result = KMS_RESULT_INVALID_REQUEST; + response.num_fds = 0; + snprintf(response.err_msg, sizeof(response.err_msg), "invalid request type %d, expected %d (%s)", request.type, KMS_REQUEST_TYPE_GET_KMS, "KMS_REQUEST_TYPE_GET_KMS"); fprintf(stderr, "kms server error: %s\n", response.err_msg); - if(send_msg_to_client(socket_fd, &response) == -1) { + if(send_msg_to_client(socket_fd, &response) == -1) fprintf(stderr, "kms server error: failed to respond to client request\n"); - break; - } + break; } } } done: - close(drm.drmfd); - close(socket_fd); + if(drm.planes) + drmModeFreePlaneResources(drm.planes); + if(drm.drmfd > 0) + close(drm.drmfd); + if(socket_fd > 0) + close(socket_fd); return res; } |