Portál AbcLinuxu, 4. prosince 2025 04:27
Řešení dotazu:
$ echo 'int main() { printf("Ret: %i\n", system("perl -e \"exit(17);\"") >> 8); }' | gcc -w -x c - && ./a.out
Ret: 17
$ echo 'int main() { printf("Ret: %i\n", system("perl -e \"17;\"") >> 8); }' | gcc -w -x c - && ./a.out
Ret: 0
suster@ubuntu-l1:~/Production/TSMReport$ ./tsm.pl
...(nejaky vystup)...
suster@ubuntu-l1:~/Production/TSMReport$ echo $?
1
suster@ubuntu-l1:~/Production/TSMReport$ ./tsm
...(nejaky vystup)...
suster@ubuntu-l1:~/Production/TSMReport$ echo $?
0
cat tsm.c
int main(int argc, char *argv[])
{
int i;
char run_this [] = "perl /home/jsuster/Production/TSMReport/tsm.pl";
setuid( 0 );
return system(run_this);
}
$ echo 'int main() { return 256; }' | gcc -w -x c - && ./a.out ; echo $?
0
Navratovy kod se da zjistit pomoci makra WEXITSTATUS (doporucuju se podivat na "man 2 wait").
Uplne nejlepsi je neco takoveho:
status = system(command);
if(WIFEXITED(status)) {
printf("Command %s exited with return code %i\n", command, WEXITSTATUS(status));
} else {
printf("Command %s ended unexpectedly\n", command,);
}
$ echo 'int main() { printf("Ret: %i\n", system("perl -e \"exit(17);\"") >> 8); }' | gcc -w -x c - && strace -f ./a.out
...
[pid 11921] exit_group(17) = ?
Process 11920 resumed
Process 11921 detached
<... waitpid resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 17}], 0) = 11921
...
cp /usr/bin/vim perl PATH=.:$PATH ./tsm
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.