how can I supply negative value to nice() and run without error even if I am not the superuser can I do
system("su");
nice(-1);
it is asking for password which I do not want?
how can I supply negative value to nice() and run without error even if I am not the superuser can I do
system("su");
nice(-1);
it is asking for password which I do not want?
If you call su
then you will be asked for password, unless you are root.
To be able to re-nice with negative values, the process will need:
You can't. That is the point of the system. No ordinary user may increase the priority of his tasks.
There is absolutely no way for an process to elevate its privileges. If it could then it would already have them.
If a process has real or saved uid elevated, then it can copy this uid into the effective uid; If it has permitted capabilities then it can copy these to the effective set, but it must have these privileges to start with. You can not magic them out of fin air (What good is a lock that you can create a key for at will).
(be careful with this, for experts only)
There are 2 ways (traditionally one, but they are both similar, and the original may one day disappear). For both it happens when you call exec. (OK I lied above, because after exec is run we are in same process, with changed privileges, but running new code).
exec
ed the userid and/or the group id will change to that of the file (possibly root), (this does not word for scripted languages on most Unixes). exec
ed the process will gain the capabilities set in the file. This is now the recommender method.
Example:
//renice.cc
#include <unistd.h>
#include <sys/capability.h>
class Renice {
cap_t original_cap_state;
cap_t can_nice_cap_state;
cap_value_t cap_list[1];
public:
Renice() {
original_cap_state = cap_get_proc();
if ( original_cap_state == NULL)
/* handle error */;
can_nice_cap_state = cap_get_proc();
if ( can_nice_cap_state == NULL)
/* handle error */;
cap_list[0] = CAP_SYS_NICE;
if (cap_set_flag(can_nice_cap_state, CAP_EFFECTIVE, 1, cap_list, CAP_SET) == -1)
/* handle error */;
}
~Renice() {
if (cap_free(original_cap_state) == -1 )
/* handle error */;
}
void new_value(int v) {
if (cap_set_proc(can_nice_cap_state) == -1)
/* handle error */;
nice (v);
/* handle error */
if (cap_set_proc(original_cap_state) == -1)
/* handle error */;
}
};
int main () {
Renice renice;
renice.new_value(-1);
nice (-2); //won't work, capability no longer set
sleep (30);
}
g++ -lcap renice.cc
sudo setcap CAP_SYS_NICE+p a.out
./a.out
nice
will be run on the current process, aftersystem
returns. – ctrl-alt-delor Jul 18 '16 at 09:49