- FILE *fp;
-
- if ((fp = fopen(pidfile, "w"))) {
- fprintf(fp, "%d\n", getpid());
- fclose(fp);
+ int fd, rc;
+
+ if ((fd = open(pidfile, O_CREAT | O_WRONLY)) >= 0) {
+ char pid[16];
+ int size = snprintf(pid, sizeof(pid), "%u\n", getpid());
+ if (write(fd, pid, size) != size) {
+ /* hard error or short write */
+ rc = errno ? : EIO;
+ } else {
+ rc = 0;
+ }
+ close(fd);