Execute shell command from other user
I want to execute shell command by user with uid 501. I run this application by root. I wrote:
Code:
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main() {
//cout<<"started as uid="<<getuid()<<", pid="<<getpid()<<endl;
pid_t result = fork();
if (result==-1) {cout<<"error fork()"<<endl;
return 1;} else cout<<"fork()="<<result<<endl;
if(result==0){
int status=setuid(501);
//cout<<"child print setuid()="<<status<<endl;
if(status>0)
{
//cout<<"child uid="<<getuid()<<endl;
//cout<<"child pid="<<getpid()<<endl;
sleep(20);
system("date >/media/disk/log");
system("whoami >/media/disk/log");
}} else if(result!=0){
//cout<<"parent uid="<<getuid()<<endl;
//cout<<"parent pid="<<getpid()<<endl;}
return 0;
}
but /media/disk/log is empty.