Excuse for unreadable.
While we wait for the move to C++, I will ask: do you get any error messages on the console? Does the user have privileges to write to /media/disk/log? Are you aware that using > as redirect will delete any previous contents of the file, meaning you'll never see the date?
Code:
[andrew@COR disk]$ ls -l
-rwxrwxrwx. 1 root root 6 Авг 23 18:37 log
I know that > will delete any previous contents of the file.
Before:
Code:
[andrew@COR disk]$ cat log
empty file
After:
Code:
[root@COR Release]# ./ssetu
started as uid=0, pid=8859
fork()=0
child print setuid()=0
fork()=8860
parent uid=0
parent pid=8859
[root@COR Release]# cat /media/disk/log
empty file
My attempt to make a code more readable:
Code:
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main() {
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);
if (status>0){
sleep(20);
system("date >/media/disk/log");
}
}
return 0;
}