Index: at.c =================================================================== --- at.c (revision 249404) +++ at.c (working copy) @@ -531,6 +531,10 @@ /* Delete every argument (job - ID) given */ int i; + int rc; + int nofJobs; + int nofDone; + int statErrno; struct stat buf; DIR *spool; struct dirent *dirent; @@ -538,6 +542,9 @@ char queue; long jobno; + nofJobs = argc - optind; + nofDone = 0; + PRIV_START if (chdir(ATJOB_DIR) != 0) @@ -553,9 +560,20 @@ while((dirent = readdir(spool)) != NULL) { PRIV_START - if (stat(dirent->d_name, &buf) != 0) - perr("cannot stat in " ATJOB_DIR); + rc = stat(dirent->d_name, &buf); + statErrno = errno; PRIV_END + /* There's a race condition between readdir above and stat here: + * another atrm process could have removed the file from the spool + * directory under our nose. If this happens, stat will set errno to + * ENOENT, which we shouldn't treat as fatal. + */ + if (rc != 0) { + if (statErrno == ENOENT) + continue; + else + perr("cannot stat in " ATJOB_DIR); + } if(sscanf(dirent->d_name, "%c%5lx%8lx", &queue, &jobno, &ctm)!=3) continue; @@ -601,9 +619,15 @@ errx(EXIT_FAILURE, "internal error, process_jobs = %d", what); } + + /* All arguments have been processed + */ + if (++nofDone == nofJobs) + goto end; } } } +end: closedir(spool); } /* delete_jobs */