2

I am trying to figure out some code that I was given. Can someone tell me what this means?

process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());

Especially the su.

Thanks!

user758362
  • 291
  • 1
  • 6
  • 18

2 Answers2

2

Used without any other kind of parameter, su changes the user ID to 0 (superuser) but does not change current path or environment.

saint
  • 54
  • 4
  • 4
    What is often overlooked, but most critically important, is that su does not change the user ID of an existing process, rather it launches a new process (probably a shell) running as the requested user ID. – Chris Stratton May 23 '11 at 17:35
  • True! To change the execution ID of the current process, a program should attempt a call to setuid(uid) uid_t uid; The su command by default starts a new shell, you can supply a command to be executed in the new shell. – saint May 23 '11 at 17:40
  • Yes, but note that setuid() cannot let a previously unprivileged process become root, which is the capability that many who try to use 'su' programmatically (especially on android) are looking for, and will not find. – Chris Stratton May 23 '11 at 17:49
1

It's a Unix command. It is used to switch active users. See for instance this manual page for details on what the command does. The Wikipedia page has some discussion about what the command name is believed to mean. I thought it was "switch user", but it wasn't that simple.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Ok, but why would someone want to switch active users in this case? – user758362 May 23 '11 at 17:28
  • To do something their current user isn't allowed to. Specifically, this code is likely starting shell process (implied) as root, and setting up a stream to send it commands, in order to have it do thing which the parent process lacks permission to do. Note that this is **not supported** in official versions of android. – Chris Stratton May 23 '11 at 17:32
  • So if I want to create an Input stream else where in another thread, (so I don't kill the app when blocking for the input) should I replicate this pattern or is there a better way to do that? – user758362 May 23 '11 at 17:35