Cleanup before exit

This commit is contained in:
Ashish Kumar Yadav 2021-08-06 21:50:09 +05:30
parent c1ff32f912
commit 28b7776611

View file

@ -178,6 +178,8 @@ updateblock(Block *block, int sigval)
switch (fork()) { switch (fork()) {
case -1: case -1:
perror("updateblock - fork"); perror("updateblock - fork");
close(fd[0]);
close(fd[1]);
cleanup(); cleanup();
exit(1); exit(1);
case 0: case 0:
@ -185,6 +187,7 @@ updateblock(Block *block, int sigval)
if (fd[1] != STDOUT_FILENO) { if (fd[1] != STDOUT_FILENO) {
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) { if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
perror("updateblock - child - dup2"); perror("updateblock - child - dup2");
close(fd[1]);
exit(1); exit(1);
} }
close(fd[1]); close(fd[1]);
@ -213,6 +216,7 @@ updateblock(Block *block, int sigval)
while (rd > 0 && (trd += rd) < CMDOUTLENGTH); while (rd > 0 && (trd += rd) < CMDOUTLENGTH);
if (rd == -1) { if (rd == -1) {
perror("updateblock - read"); perror("updateblock - read");
close(fd[0]);
cleanup(); cleanup();
exit(1); exit(1);
} }
@ -281,14 +285,17 @@ writepid()
exit(2); exit(2);
} }
perror("writepid - fcntl"); perror("writepid - fcntl");
close(fd);
exit(1); exit(1);
} }
if (ftruncate(fd, 0) == -1) { if (ftruncate(fd, 0) == -1) {
perror("writepid - ftruncate"); perror("writepid - ftruncate");
close(fd);
exit(1); exit(1);
} }
if (dprintf(fd, "%ld", (long)getpid()) < 0) { if (dprintf(fd, "%ld", (long)getpid()) < 0) {
perror("writepid - dprintf"); perror("writepid - dprintf");
close(fd);
exit(1); exit(1);
} }
} }