- if((pidout = fopen(pidfile, "w")) == NULL) {
- flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
+ pidtmp = sprintf3("%s.new", pidfile);
+ if((pidout = fopen(pidtmp, "w")) == NULL) {
+ flog(LOG_ERR, "could not open %s for writing: %s", pidtmp, strerror(errno));
+ return(1);
+ }
+ if(rename(pidtmp, pidfile)) {
+ flog(LOG_ERR, "could not overwrite %s: %s", pidfile, strerror(errno));
+ unlink(pidtmp);