User:Keks24/drafts/Non root Xorg

= To-do =


 * general
 * modesetting: Is the MIT license compatible with GPL2? o_o
 * how to verify, that the patches work? Is  is sufficient?
 * sharkoon.local
 * make of  work
 * lenovo.local
 * test  patch with
 * message kajzer, when the wiki article is done


 * wiki article
 * add reference to own, mentioning, that it is actually not supposed for others: https://codeberg.org/keks24/gentoo-overlay
 * instead of referencing to the forum, upload the files to the wiki to make them persistent
 * : https://forums.gentoo.org/viewtopic-t-1092792-postdays-0-postorder-asc-start-75.html#8478520
 * : https://forums.gentoo.org/viewtopic-p-8329654.html#8329654
 * : https://forums.gentoo.org/viewtopic-p-8376782.html#8376782
 * : https://forums.gentoo.org/viewtopic-p-8448466.html#8448466

= Actual wiki article =

https://wiki.gentoo.org/wiki/Non_root_Xorg

Handling of DRM ioctls
No management of group membership can grant the  capability needed to perform the DRM ioctls. However, a simple program named has been made available in GitHub, which must be installed with the set-user-ID on execution file mode bit set (i.e.  would show   or  ), and can perform   calls on behalf of other processes, with the help of  (from package ). The program gets open file descriptors for character special files from a UNIX domain socket via file descriptor passing ( control messages sent as ancillary data with a POSIX   call), and command-line options specify the requested ioctl. Several ebuilds suitable for a custom repository are available for the package in the Gentoo Forums

Patches are available for making the,  and  X.Org video drivers work with. The ebuilds for their corresponding packages (, and, respectively) can apply them as user patches in the usual way.

Use custom ebuild repository

 * Display ebuild file in a filebox
 * Reference to forum post: https://forums.gentoo.org/viewtopic-t-1092792-postdays-0-postorder-asc-start-75.html#8478520
 * Reference to gentoo overlay. Use at your own risk: https://codeberg.org/keks24/gentoo-overlay
 * Instructions to add the overlay manually?

Modesetting: x11-base/xorg-server

 * Instructions to apply the patch files
 * Put download link here

AMDGPU: x11-drivers/xf86-video-amdgpu

 * Instructions to apply the patch files
 * Put download link here

Radeon: x11-drivers/xf86-video-ati

 * Instructions to apply the patch files
 * Put download link here

Verification

 * Read this and get debugging information

= Files =

Modesetting: x11-base/xorg-server
{{FileBox|lang=diff|filename=/etc/portage/patches/x11-base/xorg-server/rootless_modesetting_1.20.3.diff|
 * 1) Copyright (C) 2020  Evgeny Zinoviev                                             #
 * 2) This program is free software; you can redistribute it and/or                   #
 * 3) modify it under the terms of the GNU General Public License                     #
 * 4) as published by the Free Software Foundation; either version 2                  #
 * 5) of the License, or (at your option) any later version.                          #
 * 6) This program is distributed in the hope that it will be useful,                 #
 * 7) but WITHOUT ANY WARRANTY; without even the implied warranty of                  #
 * 8) MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                   #
 * 9) GNU General Public License for more details.                                    #
 * 10) You should have received a copy of the GNU General Public License               #
 * 11) along with this program; if not, write to the Free Software                     #
 * 12) Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. #
 * 1) You should have received a copy of the GNU General Public License               #
 * 2) along with this program; if not, write to the Free Software                     #
 * 3) Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. #
 * 1) Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. #

diff --git a/hw/xfree86/drivers/modesetting/driver.c b/hw/xfree86/drivers/modesetting/driver.c index 8d29b13..1034ef9 100644 --- a/hw/xfree86/drivers/modesetting/driver.c +++ b/hw/xfree86/drivers/modesetting/driver.c @@ -34,6 +34,11 @@ +#include  +#include  +#include  +#include  +#include  @@ -62,6 +67,8 @@ +#define DRM_HACK_SOCKET_NAME "xorg_drm_master_util" + static void AdjustFrame(ScrnInfoPtr pScrn, int x, int y); static Bool CloseScreen(ScreenPtr pScreen); static Bool EnterVT(ScrnInfoPtr pScrn); @@ -1513,11 +1520,66 @@ msSharedPixmapNotifyDamage(PixmapPtr ppix) return ret; } +static int +send_fd(int sock, int fd) +{ +   // This function does the arcane magic for sending +   // file descriptors over unix domain sockets +   struct msghdr msg; +   struct iovec iov[1]; +   struct cmsghdr *cmsg = NULL; +   char ctrl_buf[CMSG_SPACE(sizeof(int))]; +   char data[1]; + +   memset(&msg, 0, sizeof(struct msghdr)); +   memset(ctrl_buf, 0, CMSG_SPACE(sizeof(int))); + +   data[0] = ' '; +   iov[0].iov_base = data; +   iov[0].iov_len = sizeof(data); + +   msg.msg_name = NULL; +   msg.msg_namelen = 0; +   msg.msg_iov = iov; +   msg.msg_iovlen = 1; +   msg.msg_controllen =  CMSG_SPACE(sizeof(int)); +   msg.msg_control = ctrl_buf; + +   cmsg = CMSG_FIRSTHDR(&msg); +   cmsg->cmsg_level = SOL_SOCKET; +   cmsg->cmsg_type = SCM_RIGHTS; +   cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + +   *((int *) CMSG_DATA(cmsg)) = fd; + +   return sendmsg(sock, &msg, 0); +} + +static void* +thread_func(void* argument) +{ +   int ret; +   int option = *(int *)argument; +   char cmd[32]; +   sprintf(cmd, "/usr/bin/drm_master_util %s -r", (!option ? "-s" : "-d")); + +   ret = system(cmd); +   if (ret == -1 || WEXITSTATUS(ret) != 0) { +       fprintf(stderr, "%s\n", strerror(errno)); +       //exit(1); +   } + +    pthread_exit(NULL); // you could also return NULL here to exit no difference +} + static Bool SetMaster(ScrnInfoPtr pScrn) {    modesettingPtr ms = modesettingPTR(pScrn); -   int ret; +   int ret = 0; +   pthread_t my_thread; +   struct sockaddr_un addr; +   int sock, conn, option = 0; if (ms->pEnt->location.type == BUS_PLATFORM && @@ -1528,10 +1590,21 @@ SetMaster(ScrnInfoPtr pScrn)    if (ms->fd_passed)         return TRUE; -    ret = drmSetMaster(ms->fd); -    if (ret) -        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "drmSetMaster failed: %s\n", -                  strerror(errno)); +    sock = socket(AF_UNIX, SOCK_STREAM, 0); +    memset(&addr, 0, sizeof(addr)); +    addr.sun_family = AF_UNIX; +    strcpy(&addr.sun_path[1], DRM_HACK_SOCKET_NAME); +    bind(sock, (struct sockaddr *)&addr, sizeof(addr)); + +    listen(sock, 1); +    pthread_create(&my_thread, NULL, thread_func, &option); + +    conn = accept(sock, NULL, 0); +    send_fd(conn, ms->fd); +    close(conn); +    close(sock); + +    pthread_join(my_thread, NULL);     return ret == 0; } @@ -1769,6 +1842,9 @@ static void LeaveVT(ScrnInfoPtr pScrn) {     modesettingPtr ms = modesettingPTR(pScrn); +    pthread_t my_thread; +    struct sockaddr_un addr; +    int sock, conn, option = 1;     xf86_hide_cursors(pScrn); @@ -1780,8 +1856,21 @@ LeaveVT(ScrnInfoPtr pScrn)         return; -    if (!ms->fd_passed) -        drmDropMaster(ms->fd); +    sock = socket(AF_UNIX, SOCK_STREAM, 0); +   memset(&addr, 0, sizeof(addr)); +   addr.sun_family = AF_UNIX; +   strcpy(&addr.sun_path[1], DRM_HACK_SOCKET_NAME); +   bind(sock, (struct sockaddr *)&addr, sizeof(addr)); + +   listen(sock, 1); +   pthread_create(&my_thread, NULL, thread_func, &option); + +   conn = accept(sock, NULL, 0); +   send_fd(conn, ms->fd); +   close(conn); +   close(sock); + +   pthread_join(my_thread, NULL); } /* }}
 * 1) endif
 * 1) include 
 * 1) include 
 * 2) include "xf86.h"
 * 3) include "xf86Priv.h"
 * 1) include "driver.h"
 * 1) ifdef XF86_PDEV_SERVER_FD
 * 1) endif

Radeon: x11-drivers/xf86-video-ati
= References and stuff =