Code to write data in to file example.txt.. which is working fine
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
File : filelimit.c below which takes input as the compiled binary of above code
In this code i am setting the file size limit of process to 10 bytes and the above code is trying to write more than 10 bytes. According to below code if a process is trying to write more than 10 bytes to file it should send the signal SIGXFSZ (see more about this here)
It is not sending here... Why??
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
void handler(int sig)
{
if(sig == SIGXFSZ)
printf("Inside handler with correct signal\n");
else
printf("Inside handler with wrong signal\n");
}
int main(int argc, char const *argv[])
{
pid_t pid;
pid = fork();
if(pid == 0)
{
signal(SIGXFSZ, handler);
char command[256];
scanf("%s", command);
int ret;
struct rlimit rl;
rl.rlim_cur = 10;
rl.rlim_max = 10;
setrlimit (RLIMIT_FSIZE, &rl);
char *envp[] = { NULL };
char *argv[] = { command , NULL };
ret = execve(command, argv, envp);
while(1);
}
else
{
printf("%d - %d\n", getpid(), pid);
signal(SIGXFSZ, handler);
while(1);
}
return 0;
}