Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Run Qt project as root
Forum Updated to NodeBB v4.3 + New Features

Run Qt project as root

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
10 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    diego-qt
    wrote on 5 Jul 2021, 14:45 last edited by
    #1

    Hello, I have a project where I need to connect to the /dev/ttyUSB0 port. For that, I do the following:

    void WTemperature::sendDataRequest()
    {
        QSerialPort *port = new QSerialPort;
        port->setPortName("/dev/ttyUSB0");
        port->setBaudRate(230400);
        port->setParity(QSerialPort::NoParity);
        port->setStopBits(QSerialPort::OneStop);
        port->setDataBits(QSerialPort::Data8);
    
        if(!port->open(QIODevice::ReadWrite))
            qDebug() << "open port error" << port->error();
        else
        {
            //send request
            const QByteArray writeData(4, '0');
            port->write(writeData);
            port->close();
        }
    
        delete port
    }
    

    The code itself compiles and runs but apparently I need root privileges to access the USB port. I get the following error every time:

    open port error QSerialPort::PermissionError
    

    I then checked the Run as user root box in the Projects menu of the Mode Selector, but then the error I got has been this:

    No protocol specified
    qt.qpa.xcb: could not connect to display :0
    qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
    This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
    
    Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
    

    I have looked at many forums for an answer but none seem to work because this error only appears when I try to run as root. Otherwise the code would run and others forum questions would have helped.

    I have also tried this Qt example
    https://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html
    The problem is that I need to run with sudo or else I get this error:

    QIODevice::write (QSerialPort): device not open
    Failed to write the data to port ttyUSB0, error: Permission denied
    Operation timed out for port ttyUSB0, error: Permission denied
    

    It is clear to me that the problem is the permission. My question is, is it possible to run a project as root? I hope it is more complex that just do a chmod in /dev/ttyUSB0 (I have not tried because I didn't want to mess up with the USB folder in root).
    By the way, I am using Qt 5.15.2, if you need more information about my code, feel free to ask. This is my first post ever so I may have forgotten to add obvious pieces of information.

    J 1 Reply Last reply 5 Jul 2021, 14:49
    0
    • D diego-qt
      5 Jul 2021, 14:45

      Hello, I have a project where I need to connect to the /dev/ttyUSB0 port. For that, I do the following:

      void WTemperature::sendDataRequest()
      {
          QSerialPort *port = new QSerialPort;
          port->setPortName("/dev/ttyUSB0");
          port->setBaudRate(230400);
          port->setParity(QSerialPort::NoParity);
          port->setStopBits(QSerialPort::OneStop);
          port->setDataBits(QSerialPort::Data8);
      
          if(!port->open(QIODevice::ReadWrite))
              qDebug() << "open port error" << port->error();
          else
          {
              //send request
              const QByteArray writeData(4, '0');
              port->write(writeData);
              port->close();
          }
      
          delete port
      }
      

      The code itself compiles and runs but apparently I need root privileges to access the USB port. I get the following error every time:

      open port error QSerialPort::PermissionError
      

      I then checked the Run as user root box in the Projects menu of the Mode Selector, but then the error I got has been this:

      No protocol specified
      qt.qpa.xcb: could not connect to display :0
      qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
      This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
      
      Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
      

      I have looked at many forums for an answer but none seem to work because this error only appears when I try to run as root. Otherwise the code would run and others forum questions would have helped.

      I have also tried this Qt example
      https://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html
      The problem is that I need to run with sudo or else I get this error:

      QIODevice::write (QSerialPort): device not open
      Failed to write the data to port ttyUSB0, error: Permission denied
      Operation timed out for port ttyUSB0, error: Permission denied
      

      It is clear to me that the problem is the permission. My question is, is it possible to run a project as root? I hope it is more complex that just do a chmod in /dev/ttyUSB0 (I have not tried because I didn't want to mess up with the USB folder in root).
      By the way, I am using Qt 5.15.2, if you need more information about my code, feel free to ask. This is my first post ever so I may have forgotten to add obvious pieces of information.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 5 Jul 2021, 14:49 last edited by
      #2

      @diego-qt You should rather add your user to the group which is set on the device file (ttyUSB0), then the user should be able to access it.
      The problem you see if you run as root is because by default root (with sudo or su) has no access to X11 server for security reasons (this is not Qt issue).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • D Offline
        D Offline
        diego-qt
        wrote on 6 Jul 2021, 07:37 last edited by
        #3

        I don't think that's the problem because I am able to run the example I mentioned (https://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html) with root privileges (aka sudo ./cwriteasync <...>).
        There must be something I'm doing differently.

        J 1 Reply Last reply 6 Jul 2021, 07:48
        0
        • D diego-qt
          6 Jul 2021, 07:37

          I don't think that's the problem because I am able to run the example I mentioned (https://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html) with root privileges (aka sudo ./cwriteasync <...>).
          There must be something I'm doing differently.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 6 Jul 2021, 07:48 last edited by jsulm 7 Jun 2021, 07:48
          #4

          @diego-qt That example is a command line tool without GUI as far as I can see. So, it does not need X server.
          Your app is a GUI application which requires the X server. So, it IS the reason...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply 6 Jul 2021, 08:53
          3
          • J jsulm
            6 Jul 2021, 07:48

            @diego-qt That example is a command line tool without GUI as far as I can see. So, it does not need X server.
            Your app is a GUI application which requires the X server. So, it IS the reason...

            D Offline
            D Offline
            diego-qt
            wrote on 6 Jul 2021, 08:53 last edited by
            #5

            @jsulm Ok, I see. It's true that the Qt example is a console application. But I find it difficult to understand the relation between X11 and my application.
            If I got it right, my application needs access to X11 because, since it is a GUI, it automatically runs a X Window server? A GUI application will always be related to X? Sorry I didn't know the existence of X before yesterday :/

            J 1 Reply Last reply 6 Jul 2021, 09:12
            0
            • D diego-qt
              6 Jul 2021, 08:53

              @jsulm Ok, I see. It's true that the Qt example is a console application. But I find it difficult to understand the relation between X11 and my application.
              If I got it right, my application needs access to X11 because, since it is a GUI, it automatically runs a X Window server? A GUI application will always be related to X? Sorry I didn't know the existence of X before yesterday :/

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 6 Jul 2021, 09:12 last edited by
              #6

              @diego-qt said in Run Qt project as root:

              it automatically runs a X Window server?

              No, the X server is already running when you're logging in on your machine. If you then start your app it connects to the X server.
              "A GUI application will always be related to X?" - it depends. X is still the standard in Linux/UNIX but Wayland is taking over. By default Qt applications use X server on Linux.
              Did you check which group is set on /dev/ttyUSB0? You could simply add your user to that group, then there is no need for root.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 1 Reply Last reply 6 Jul 2021, 09:31
              3
              • J jsulm
                6 Jul 2021, 09:12

                @diego-qt said in Run Qt project as root:

                it automatically runs a X Window server?

                No, the X server is already running when you're logging in on your machine. If you then start your app it connects to the X server.
                "A GUI application will always be related to X?" - it depends. X is still the standard in Linux/UNIX but Wayland is taking over. By default Qt applications use X server on Linux.
                Did you check which group is set on /dev/ttyUSB0? You could simply add your user to that group, then there is no need for root.

                D Offline
                D Offline
                diego-qt
                wrote on 6 Jul 2021, 09:31 last edited by
                #7

                @jsulm the group is dialout, but I'm being told that it might not be the best way to solve the problem because what I need is that the application runs without the need to adjust specific parameters in every PC that will run the program. Isn't there a way to automatically access /dev/ttyUSB0 ?

                J JonBJ 2 Replies Last reply 6 Jul 2021, 11:05
                0
                • D diego-qt
                  6 Jul 2021, 09:31

                  @jsulm the group is dialout, but I'm being told that it might not be the best way to solve the problem because what I need is that the application runs without the need to adjust specific parameters in every PC that will run the program. Isn't there a way to automatically access /dev/ttyUSB0 ?

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 6 Jul 2021, 11:05 last edited by
                  #8

                  @diego-qt said in Run Qt project as root:

                  what I need is that the application runs without the need to adjust specific parameters in every PC that will run the program.

                  It is even worse to ask users to run the app as root!
                  Especially if it requires X server which does not allow access for applications running with sudo/su - you would need to change the configuration on every PC to make this work!
                  So, my advice is still the same: add the users to the group dialout, this is the correct way to solve this...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  5
                  • D diego-qt
                    6 Jul 2021, 09:31

                    @jsulm the group is dialout, but I'm being told that it might not be the best way to solve the problem because what I need is that the application runs without the need to adjust specific parameters in every PC that will run the program. Isn't there a way to automatically access /dev/ttyUSB0 ?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 6 Jul 2021, 16:00 last edited by JonB 7 Jun 2021, 16:01
                    #9

                    @diego-qt said in Run Qt project as root:

                    the group is dialout, but I'm being told that it might not be the best way to solve the problem because what I need is that the application runs without the need to adjust specific parameters in every PC that will run the program. Isn't there a way to automatically access /dev/ttyUSB0 ?

                    No, there isn't an "automatic way". The device has permissions on it. It (apparently) requires membership of group dialout to access. Permissions are permissions, and they are there for a reason! So the running user should be a member of that group. (Or, you change the permissions on the device to allow access by any user, regardless of group.) I don't know who's saying you should not have to alter anything to achieve that...!

                    In case anyone comments. Linux programs can be run "setuid" or "setgid" to grant those permissions. That could apply to your executable. However, those bits must be set on the executable after distribution/installation, so it still requires changes on the target machine.

                    As @jsulm says

                    So, my advice is still the same: add the users to the group dialout, this is the correct way to solve this...

                    1 Reply Last reply
                    2
                    • D Offline
                      D Offline
                      diego-qt
                      wrote on 7 Jul 2021, 08:10 last edited by
                      #10

                      Ok then, thank you for this clarification. I will follow your advice.

                      1 Reply Last reply
                      0

                      1/10

                      5 Jul 2021, 14:45

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved