Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Requesting Root on Qt Android
Forum Updated to NodeBB v4.3 + New Features

Requesting Root on Qt Android

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
6 Posts 2 Posters 2.6k 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.
  • espocjoE Offline
    espocjoE Offline
    espocjo
    wrote on last edited by
    #1

    Hi all.
    I have a rooted Android device, and am trying to run my Qt app with root permissions.
    I've looked around, and it seems that the recommended way to do this is by running the following line of Java:

    Process process = Runtime.getRuntime().exec("su");
    

    From what I understand, it creates a new Java thread as root, and the parent thread inherits this permission.

    I have attempted to convert this call into a QAndroidJniObject, using the following C++ code:

    QAndroidJniObject process = QAndroidJniObject::callStaticObjectMethod("java/lang/Runtime", "getRuntime().exec", "(Ljava/lang/Process)Ljava/lang/String;", "su");
    

    Unfortuantely, it just does nothing. No prompt for root access, not even an error message!
    I'm guessing this is because I've treated "getRuntime().exec" as if it were a class, whereas from the look of things, getRuntime is a function and exec is called on the return value of that.
    Is there a way to deal with this in Qt? If not, does anyone know a way of requesting root using native code?

    Cheers,
    ~Chris

    jsulmJ 1 Reply Last reply
    0
    • espocjoE espocjo

      Hi all.
      I have a rooted Android device, and am trying to run my Qt app with root permissions.
      I've looked around, and it seems that the recommended way to do this is by running the following line of Java:

      Process process = Runtime.getRuntime().exec("su");
      

      From what I understand, it creates a new Java thread as root, and the parent thread inherits this permission.

      I have attempted to convert this call into a QAndroidJniObject, using the following C++ code:

      QAndroidJniObject process = QAndroidJniObject::callStaticObjectMethod("java/lang/Runtime", "getRuntime().exec", "(Ljava/lang/Process)Ljava/lang/String;", "su");
      

      Unfortuantely, it just does nothing. No prompt for root access, not even an error message!
      I'm guessing this is because I've treated "getRuntime().exec" as if it were a class, whereas from the look of things, getRuntime is a function and exec is called on the return value of that.
      Is there a way to deal with this in Qt? If not, does anyone know a way of requesting root using native code?

      Cheers,
      ~Chris

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @espocjo As far as I know Runtime.getRuntime().exec executes the command in a new process, not in a thread in your process (how should this even work?). Since su is executed in another process I don't think your process will become root. Usually a program is started as root (like: sudo ./app).

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

      espocjoE 1 Reply Last reply
      0
      • espocjoE Offline
        espocjoE Offline
        espocjo
        wrote on last edited by
        #3

        You're not wrong.
        After working out the intricacies of Java and discovering ANDROID_PACKAGE_SOURCE_DIR, I finally managed to get that command to work.

        Of course, it did nothing. I have no idea what it does; I was just copying what I'd read on Stack Exchange.
        (Re: multiple threads in a single process, Windows can have multi-threaded .exes. Evidently, it seems like Android doesn't! Please excuse me, I just spent half a day trying to get "hello world" printed over logcat.)

        jsulmJ 1 Reply Last reply
        0
        • jsulmJ jsulm

          @espocjo As far as I know Runtime.getRuntime().exec executes the command in a new process, not in a thread in your process (how should this even work?). Since su is executed in another process I don't think your process will become root. Usually a program is started as root (like: sudo ./app).

          espocjoE Offline
          espocjoE Offline
          espocjo
          wrote on last edited by
          #4

          @jsulm
          Do you know of any command that an app could use to elevate itself? Or am I going about it the wrong way?

          1 Reply Last reply
          0
          • espocjoE espocjo

            You're not wrong.
            After working out the intricacies of Java and discovering ANDROID_PACKAGE_SOURCE_DIR, I finally managed to get that command to work.

            Of course, it did nothing. I have no idea what it does; I was just copying what I'd read on Stack Exchange.
            (Re: multiple threads in a single process, Windows can have multi-threaded .exes. Evidently, it seems like Android doesn't! Please excuse me, I just spent half a day trying to get "hello world" printed over logcat.)

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @espocjo One process executes one executable, it doesn't matter how many threads. You cannot execute one executable in one thread and another executable in another thread in the same process.
            Can't you start your app with sudo? Or use setuid: https://en.wikipedia.org/wiki/Setuid

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

            espocjoE 1 Reply Last reply
            0
            • jsulmJ jsulm

              @espocjo One process executes one executable, it doesn't matter how many threads. You cannot execute one executable in one thread and another executable in another thread in the same process.
              Can't you start your app with sudo? Or use setuid: https://en.wikipedia.org/wiki/Setuid

              espocjoE Offline
              espocjoE Offline
              espocjo
              wrote on last edited by
              #6

              @jsulm

              Apparently setuid was disabled in Android 4.3 for security reasons.
              (And apparently Java's "Process" is not a new thread).
              I don't know of any way to launch an app with su command. If there is one, it doesn't seem simple.

              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