Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calling ioctl() under linux - problem
QtWS25 Last Chance

Calling ioctl() under linux - problem

Scheduled Pinned Locked Moved General and Desktop
14 Posts 5 Posters 11.4k Views
  • 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.
  • P Offline
    P Offline
    paa123456
    wrote on last edited by
    #3

    Hello Franzk,

    I did found the issue -
    linux kernel headers have a macro -
    _IOWR(....)
    which calculates the ioctl number passed to - ioctl()

    It seems gcc compiler calculates it properly.
    However qt (or g++) makes the ioctl number with 0xa0 added.
    I am not going to dig deeper, I just did subtract 0xa0 from the number in my qt program.

    I suspect the issue is with qt - for ex. the size of 'int' variable is 64bit, versus normally 32bit in most other programs.

    Paul.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #4

      Qt has no influence on the size of integers. I think it is a rather blunt solution to just subtract 0xa0 from your ioctl number, but if it works for you, I'm not to complain :P.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #5

        Please do not run Qt Creator as root! Running big applications with plugins and whatnot as root is never a good idea.

        You can always start your application as root inside creator: Just add a custom executable run configuration and call a script that does the sudo and then starts your application.

        I would go deeper and try to figure out this issue: I see no reason why g++ should calculate another offset as gcc. Maybe it is a compiler bug or some issue with include files (you did use extern "C", didn't you?). I would be afraid that this workaround will fail after some upgrade of ubuntu (when the root cause is fixed:-).

        1 Reply Last reply
        0
        • P Offline
          P Offline
          paa123456
          wrote on last edited by
          #6

          Hello Tobias,

          If I run qt regularly , not as root, and I run in debug mode to test my app - can I do 'open' of a driver?
          Normally it fails if run not as root .

          How do I do this -

          add a custom executable run configuration and call a script that does the sudo and then starts your application

          Regarding my fix - once the app is built it will run ok regardless of fixes in ubuntu or qt.
          I will pay attention if rebuilding the app with fixed ubuntu or qt.

          Paul.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Franzk
            wrote on last edited by
            #7

            You can also set the permissions for the specific device so that your development user has the necessary rights. This is a much safer approach generally.

            I find it hard to believe that you have to subtract 0xa0 from your file descriptor in order for the ioctl to work and that it is a bug in ubuntu. Please post your code, preferably your entire project (tarred of course).

            "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on last edited by
              #8

              Franzk: You are right, this is most likely not a bug in ubuntu. I just wanted to point out that this will come back to bite paa123456 sooner or later if not properly investigated.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #9

                And nobody thinks about some brave users who will crash their files by using this program. It is scary that such software will be out in the wild!

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Franzk
                  wrote on last edited by
                  #10

                  I was actually kind of hoping the program wouldn't be released into the wild like that.

                  "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    paa123456
                    wrote on last edited by
                    #11

                    Hi ,

                    I wrote -

                    I did found the issue – linux kernel headers have a macro – _IOWR(….)
                    which calculates the ioctl number passed to – ioctl()

                    It seems gcc compiler calculates it properly.
                    However qt (or g++) makes the ioctl number with 0xa0 added.
                    I am not going to dig deeper, I just did subtract 0xa0 from the number in my qt program.

                    Here is the simplified code -

                    @
                    #include <stdio.h>

                    #include <fcntl.h> /* open /
                    #include <unistd.h> /
                    exit /
                    #include <sys/ioctl.h> /
                    ioctl */

                    #include <pthread.h>

                    #include <sys/stat.h>
                    #include <sys/types.h>

                    #include <sys/mman.h>
                    #include <sys/syscall.h>
                    #include <sys/utsname.h>

                    #define BYTE unsigned char
                    #define WORD unsigned short
                    #define DWORD unsigned int

                    #define TRUE 1
                    #define FALSE 0

                    #define FILE_DEVICE_UNKNOWN
                    #define METHOD_BUFFERED
                    #define FILE_ANY_ACCESS
                    #define CTL_CODE(a, b,c, d) _IOWR(0, b, int) // !!! linux specific |0x4000

                    // usl related
                    #define ARS_IOCTL_INDEX 0x000

                    #define IOCTL_ARS_IO_INP CTL_CODE(FILE_DEVICE_UNKNOWN,
                    ARS_IOCTL_INDEX + 0x2b,
                    METHOD_BUFFERED,
                    FILE_ANY_ACCESS)

                    int main ()
                    {
                    DWORD dw;
                    // void *drvadr;

                    dw = IOCTL_ARS_IO_INP;
                    printf("\n IOCTL_ARS_IO_INP = %x\n", dw);

                    return 0;
                    

                    }
                    @

                    You can -

                    • save it as file and build it with gcc
                    • make a simple qtcreator app and include code

                    Paul.

                    [edit: code highlighted / Denis Kormalev]

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      Franzk
                      wrote on last edited by
                      #12

                      Did you try to compile this yourself?

                      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        Franzk
                        wrote on last edited by
                        #13

                        Some copy issue that was. Both gcc and g++ give the same result for me. (Gentoo, gcc 4.5.2)

                        "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          DenisKormalev
                          wrote on last edited by
                          #14

                          paa123456, please use @ to highlight code

                          1 Reply Last reply
                          0

                          • Login

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