/* forktest_bomb: fork children (kept alive) until fork() fails, then report.
 * Run as root to prove the leaked nprocs has collapsed root's fork capacity.
 * On a clean system root can fork ~maxproc children; after the DF-0032 leak,
 * far fewer. When nprocs>=maxproc, even root's fork returns EAGAIN. */
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/sysctl.h>
int main(void){
    int ok=0, eagain=0;
    for(int i=0;i<100000;i++){
        pid_t p=fork();
        if(p==0){ pause(); _exit(0); }      /* child stays alive */
        if(p>0){ ok++; }
        else if(errno==EAGAIN){ eagain++; if(eagain<=3) printf("forktest_bomb: root fork() EAGAIN after %d children (errno=%d %s)\n",ok,errno,strerror(errno)); if(eagain>=3) break; }
        else { printf("forktest_bomb: errno=%d %s\n",errno,strerror(errno)); break; }
    }
    printf("forktest_bomb: ROOT RESULT ok=%d eagain=%d  (clean system would allow ~%d)\n",
           ok, eagain, 4036);
    fflush(stdout);
    /* keep children alive briefly so the wedge is observable */
    sleep(3);
    kill(0,SIGTERM);
    while(waitpid(-1,NULL,0)>0);
    return 0;
}
