- /*
- * Try and create a temporary file in the same directory so
- * that in case of error we don't overwrite the cache file.
- * If the cache file doesn't yet exist, it isn't a regular
- * file (e.g. /dev/null or a socket), or we couldn't create
- * a temporary file then we open it directly.
- */
- if (ret == 0 && S_ISREG(st.st_mode)) {
- tmp = malloc(strlen(filename) + 8);
- if (tmp) {
- sprintf(tmp, "%s-XXXXXX", filename);
- fd = mkstemp(tmp);
- if (fd >= 0) {
- file = fdopen(fd, "w");
- opened = tmp;
- }
- fchmod(fd, 0644);
+ /*
+ * Try and create a temporary file in the same directory so
+ * that in case of error we don't overwrite the cache file.
+ * If the cache file doesn't yet exist, it isn't a regular
+ * file (e.g. /dev/null or a socket), or we couldn't create
+ * a temporary file then we open it directly.
+ */
+ if (ret == 0 && S_ISREG(st.st_mode)) {
+ tmp = malloc(strlen(filename) + 8);
+ if (tmp) {
+ mode_t save_umask = umask(022);
+ sprintf(tmp, "%s-XXXXXX", filename);
+ fd = mkstemp(tmp);
+ umask(save_umask);
+ if (fd >= 0) {
+ file = fdopen(fd, "w");
+ opened = tmp;