tags 1092269 + patch

Hey everyone,

I took a look at the source code and came up with a working solution that can 
be seen in the attached patch.

Unfortunately, I do not have a Salsa account yet, hence the patch file.

Regards,

Christian Goeschel Ndjomouo
B.Sc. Software Engineering
Western Governors University

Email       [email protected]


Description: Clean up temporary files
 Temporary files are not unlinked even after all file descriptors are closed.
Author: Christian Goeschel Ndjomouo <[email protected]>
Forwarded: not-needed
Origin: vendor, https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1092269
Bug-Debian: https://bugs.debian.org/1092269
Last-Update: 2025-04-20
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- a/global.h
+++ b/global.h
@@ -59,6 +59,7 @@
    char *ident;
    char *command;
    char *mailto;
+   char *temp_file_path;
 
    int tab_line;
    int arg_num;
--- a/runjob.c
+++ b/runjob.c
@@ -36,22 +36,30 @@
 #include "global.h"
 
 static int
-temp_file()
+temp_file(job_rec *jr)
 /* Open a temporary file and return its file descriptor */
 {
     const int max_retries = 50;
-    char template[] = "/tmp/anacron-XXXXXX";
+    char *name;
+    char template[] = "/tmp/anacron-XXXXXXX";
     int fd, i;
 
+    name = NULL;
     i = 0;
     do
     {
-	i++;
-	fd = mkstemp(template);
+        i++;
+        free(name);
+        name = mktemp(template);
+        if (name == NULL) die("Can't find a unique temporary filename");
+        fd = open(name, O_RDWR | O_CREAT | O_EXCL | O_APPEND,
+            S_IRUSR | S_IWUSR);
+
 	/* I'm not sure we actually need to be so persistent here */
-    } while (fd == -1 && i < max_retries);
+    } while (fd == -1 && errno == EEXIST && i < max_retries);
     
     if (fd == -1) die_e("Failed to create and open unique temporary filename");
+    jr->temp_file_path = strdup(name);
     fcntl(fd, F_SETFD, 1);    /* set close-on-exec flag */
     return fd;
 }
@@ -226,7 +234,7 @@
 	    jr->mailto = username ();
 
     /* create temporary file for stdout and stderr of the job */
-    fd = jr->output_fd = temp_file();
+    fd = jr->output_fd = temp_file(jr);
     /* write mail header */
     xwrite(fd, "From: ");
     xwrite(fd, "Anacron <");
@@ -291,6 +299,7 @@
     jr->job_pid = 0;
     running_jobs--;
     if (mail_output) launch_mailer(jr);
+    if (unlink(jr->temp_file_path)) die_e("Can't unlink temporary file");
     xclose(jr->output_fd);
 }
 

Reply via email to