pacemaker  2.0.5-ba59be7122
Scalable High-Availability cluster resource manager
io.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2020 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
12 #ifndef _GNU_SOURCE
13 # define _GNU_SOURCE
14 #endif
15 
16 #include <sys/param.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/resource.h>
20 
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <fcntl.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <pwd.h>
30 #include <grp.h>
31 
32 #include <crm/crm.h>
33 #include <crm/common/util.h>
34 
44 int
45 pcmk__build_path(const char *path_c, mode_t mode)
46 {
47  int offset = 1, len = 0;
48  int rc = pcmk_rc_ok;
49  char *path = strdup(path_c);
50 
51  // cppcheck seems not to understand the abort logic in CRM_CHECK
52  // cppcheck-suppress memleak
53  CRM_CHECK(path != NULL, return -ENOMEM);
54  for (len = strlen(path); offset < len; offset++) {
55  if (path[offset] == '/') {
56  path[offset] = 0;
57  if ((mkdir(path, mode) < 0) && (errno != EEXIST)) {
58  rc = errno;
59  goto done;
60  }
61  path[offset] = '/';
62  }
63  }
64  if ((mkdir(path, mode) < 0) && (errno != EEXIST)) {
65  rc = errno;
66  }
67 done:
68  free(path);
69  return rc;
70 }
71 
80 void
81 crm_build_path(const char *path_c, mode_t mode)
82 {
83  int rc = pcmk__build_path(path_c, mode);
84 
85  if (rc != pcmk_rc_ok) {
86  crm_err("Could not create directory '%s': %s",
87  path_c, pcmk_rc_str(rc));
88  }
89 }
90 
103 int
104 pcmk__real_path(const char *path, char **resolved_path)
105 {
106  CRM_CHECK((path != NULL) && (resolved_path != NULL), return EINVAL);
107 
108 #if _POSIX_VERSION >= 200809L
109  /* Recent C libraries can dynamically allocate memory as needed */
110  *resolved_path = realpath(path, NULL);
111  return (*resolved_path == NULL)? errno : pcmk_rc_ok;
112 
113 #elif defined(PATH_MAX)
114  /* Older implementations require pre-allocated memory */
115  /* (this is less desirable because PATH_MAX may be huge or not defined) */
116  *resolved_path = malloc(PATH_MAX);
117  if ((*resolved_path == NULL) || (realpath(path, *resolved_path) == NULL)) {
118  return errno;
119  }
120  return pcmk_rc_ok;
121 #else
122  *resolved_path = NULL;
123  return ENOTSUP;
124 #endif
125 }
126 
139 char *
140 pcmk__series_filename(const char *directory, const char *series, int sequence,
141  bool bzip)
142 {
143  CRM_ASSERT((directory != NULL) && (series != NULL));
144  return crm_strdup_printf("%s/%s-%d.%s", directory, series, sequence,
145  (bzip? "bz2" : "raw"));
146 }
147 
158 int
159 pcmk__read_series_sequence(const char *directory, const char *series,
160  unsigned int *seq)
161 {
162  int rc;
163  FILE *fp = NULL;
164  char *series_file = NULL;
165 
166  if ((directory == NULL) || (series == NULL) || (seq == NULL)) {
167  return EINVAL;
168  }
169 
170  series_file = crm_strdup_printf("%s/%s.last", directory, series);
171  fp = fopen(series_file, "r");
172  if (fp == NULL) {
173  rc = errno;
174  crm_debug("Could not open series file %s: %s",
175  series_file, strerror(rc));
176  free(series_file);
177  return rc;
178  }
179  errno = 0;
180  if (fscanf(fp, "%u", seq) != 1) {
181  rc = (errno == 0)? pcmk_rc_unknown_format : errno;
182  crm_debug("Could not read sequence number from series file %s: %s",
183  series_file, pcmk_rc_str(rc));
184  fclose(fp);
185  free(series_file);
186  return rc;
187  }
188  fclose(fp);
189  crm_trace("Found last sequence number %u in series file %s",
190  *seq, series_file);
191  free(series_file);
192  return pcmk_rc_ok;
193 }
194 
206 void
207 pcmk__write_series_sequence(const char *directory, const char *series,
208  unsigned int sequence, int max)
209 {
210  int rc = 0;
211  FILE *file_strm = NULL;
212  char *series_file = NULL;
213 
214  CRM_CHECK(directory != NULL, return);
215  CRM_CHECK(series != NULL, return);
216 
217  if (max == 0) {
218  return;
219  }
220  if (max > 0 && sequence >= max) {
221  sequence = 0;
222  }
223 
224  series_file = crm_strdup_printf("%s/%s.last", directory, series);
225  file_strm = fopen(series_file, "w");
226  if (file_strm != NULL) {
227  rc = fprintf(file_strm, "%u", sequence);
228  if (rc < 0) {
229  crm_perror(LOG_ERR, "Cannot write to series file %s", series_file);
230  }
231 
232  } else {
233  crm_err("Cannot open series file %s for writing", series_file);
234  }
235 
236  if (file_strm != NULL) {
237  fflush(file_strm);
238  fclose(file_strm);
239  }
240 
241  crm_trace("Wrote %d to %s", sequence, series_file);
242  free(series_file);
243 }
244 
256 int
257 pcmk__chown_series_sequence(const char *directory, const char *series,
258  uid_t uid, gid_t gid)
259 {
260  char *series_file = NULL;
261  int rc = pcmk_rc_ok;
262 
263  if ((directory == NULL) || (series == NULL)) {
264  return EINVAL;
265  }
266  series_file = crm_strdup_printf("%s/%s.last", directory, series);
267  if (chown(series_file, uid, gid) < 0) {
268  rc = errno;
269  }
270  free(series_file);
271  return rc;
272 }
273 
274 static bool
275 pcmk__daemon_user_can_write(const char *target_name, struct stat *target_stat)
276 {
277  struct passwd *sys_user = NULL;
278 
279  errno = 0;
280  sys_user = getpwnam(CRM_DAEMON_USER);
281  if (sys_user == NULL) {
282  crm_notice("Could not find user %s: %s",
284  return FALSE;
285  }
286  if (target_stat->st_uid != sys_user->pw_uid) {
287  crm_notice("%s is not owned by user %s " CRM_XS " uid %d != %d",
288  target_name, CRM_DAEMON_USER, sys_user->pw_uid,
289  target_stat->st_uid);
290  return FALSE;
291  }
292  if ((target_stat->st_mode & (S_IRUSR | S_IWUSR)) == 0) {
293  crm_notice("%s is not readable and writable by user %s "
294  CRM_XS " st_mode=0%lo",
295  target_name, CRM_DAEMON_USER,
296  (unsigned long) target_stat->st_mode);
297  return FALSE;
298  }
299  return TRUE;
300 }
301 
302 static bool
303 pcmk__daemon_group_can_write(const char *target_name, struct stat *target_stat)
304 {
305  struct group *sys_grp = NULL;
306 
307  errno = 0;
308  sys_grp = getgrnam(CRM_DAEMON_GROUP);
309  if (sys_grp == NULL) {
310  crm_notice("Could not find group %s: %s",
312  return FALSE;
313  }
314 
315  if (target_stat->st_gid != sys_grp->gr_gid) {
316  crm_notice("%s is not owned by group %s " CRM_XS " uid %d != %d",
317  target_name, CRM_DAEMON_GROUP,
318  sys_grp->gr_gid, target_stat->st_gid);
319  return FALSE;
320  }
321 
322  if ((target_stat->st_mode & (S_IRGRP | S_IWGRP)) == 0) {
323  crm_notice("%s is not readable and writable by group %s "
324  CRM_XS " st_mode=0%lo",
325  target_name, CRM_DAEMON_GROUP,
326  (unsigned long) target_stat->st_mode);
327  return FALSE;
328  }
329  return TRUE;
330 }
331 
346 bool
347 pcmk__daemon_can_write(const char *dir, const char *file)
348 {
349  int s_res = 0;
350  struct stat buf;
351  char *full_file = NULL;
352  const char *target = NULL;
353 
354  // Caller must supply directory
355  CRM_ASSERT(dir != NULL);
356 
357  // If file is given, check whether it exists as a regular file
358  if (file != NULL) {
359  full_file = crm_strdup_printf("%s/%s", dir, file);
360  target = full_file;
361 
362  s_res = stat(full_file, &buf);
363  if (s_res < 0) {
364  crm_notice("%s not found: %s", target, pcmk_strerror(errno));
365  free(full_file);
366  full_file = NULL;
367  target = NULL;
368 
369  } else if (S_ISREG(buf.st_mode) == FALSE) {
370  crm_err("%s must be a regular file " CRM_XS " st_mode=0%lo",
371  target, (unsigned long) buf.st_mode);
372  free(full_file);
373  return false;
374  }
375  }
376 
377  // If file is not given, ensure dir exists as directory
378  if (target == NULL) {
379  target = dir;
380  s_res = stat(dir, &buf);
381  if (s_res < 0) {
382  crm_err("%s not found: %s", dir, pcmk_strerror(errno));
383  return false;
384 
385  } else if (S_ISDIR(buf.st_mode) == FALSE) {
386  crm_err("%s must be a directory " CRM_XS " st_mode=0%lo",
387  dir, (unsigned long) buf.st_mode);
388  return false;
389  }
390  }
391 
392  if (!pcmk__daemon_user_can_write(target, &buf)
393  && !pcmk__daemon_group_can_write(target, &buf)) {
394 
395  crm_err("%s must be owned and writable by either user %s or group %s "
396  CRM_XS " st_mode=0%lo",
398  (unsigned long) buf.st_mode);
399  free(full_file);
400  return false;
401  }
402 
403  free(full_file);
404  return true;
405 }
406 
414 void
416 {
417  int fd;
418  DIR *directory;
419 
420  directory = opendir(name);
421  if (directory == NULL) {
422  crm_perror(LOG_ERR, "Could not open %s for syncing", name);
423  return;
424  }
425 
426  fd = dirfd(directory);
427  if (fd < 0) {
428  crm_perror(LOG_ERR, "Could not obtain file descriptor for %s", name);
429  return;
430  }
431 
432  if (fsync(fd) < 0) {
433  crm_perror(LOG_ERR, "Could not sync %s", name);
434  }
435  if (closedir(directory) < 0) {
436  crm_perror(LOG_ERR, "Could not close %s after fsync", name);
437  }
438 }
439 
450 int
451 pcmk__file_contents(const char *filename, char **contents)
452 {
453  FILE *fp;
454  int length, read_len;
455  int rc = pcmk_rc_ok;
456 
457  if ((filename == NULL) || (contents == NULL)) {
458  return EINVAL;
459  }
460 
461  fp = fopen(filename, "r");
462  if ((fp == NULL) || (fseek(fp, 0L, SEEK_END) < 0)) {
463  rc = errno;
464  goto bail;
465  }
466 
467  length = ftell(fp);
468  if (length < 0) {
469  rc = errno;
470  goto bail;
471  }
472 
473  if (length == 0) {
474  *contents = NULL;
475  } else {
476  *contents = calloc(length + 1, sizeof(char));
477  if (*contents == NULL) {
478  rc = errno;
479  goto bail;
480  }
481  rewind(fp);
482  read_len = fread(*contents, 1, length, fp); /* Coverity: False positive */
483  if (read_len != length) {
484  free(*contents);
485  *contents = NULL;
486  rc = EIO;
487  }
488  }
489 
490 bail:
491  if (fp != NULL) {
492  fclose(fp);
493  }
494  return rc;
495 }
496 
506 int
507 pcmk__write_sync(int fd, const char *contents)
508 {
509  int rc = 0;
510  FILE *fp = fdopen(fd, "w");
511 
512  if (fp == NULL) {
513  return errno;
514  }
515  if ((contents != NULL) && (fprintf(fp, "%s", contents) < 0)) {
516  rc = EIO;
517  }
518  if (fflush(fp) != 0) {
519  rc = errno;
520  }
521  if (fsync(fileno(fp)) < 0) {
522  rc = errno;
523  }
524  fclose(fp);
525  return rc;
526 }
527 
536 int
538 {
539  int flag = fcntl(fd, F_GETFL);
540 
541  if (flag < 0) {
542  return errno;
543  }
544  if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
545  return errno;
546  }
547  return pcmk_rc_ok;
548 }
549 
559 const char *
561 {
562  const char *dir = getenv("TMPDIR");
563 
564  return (dir && (*dir == '/'))? dir : "/tmp";
565 }
566 
577 void
579 {
580  DIR *dir;
581  struct rlimit rlim;
582  rlim_t max_fd;
583  int min_fd = (all? 0 : (STDERR_FILENO + 1));
584 
585  /* Find the current process's (soft) limit for open files. getrlimit()
586  * should always work, but have a fallback just in case.
587  */
588  if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
589  max_fd = rlim.rlim_cur - 1;
590  } else {
591  long conf_max = sysconf(_SC_OPEN_MAX);
592 
593  max_fd = (conf_max > 0)? conf_max : 1024;
594  }
595 
596  /* /proc/self/fd (on Linux) or /dev/fd (on most OSes) contains symlinks to
597  * all open files for the current process, named as the file descriptor.
598  * Use this if available, because it's more efficient than a shotgun
599  * approach to closing descriptors.
600  */
601 #if SUPPORT_PROCFS
602  dir = opendir("/proc/self/fd");
603  if (dir == NULL) {
604  dir = opendir("/dev/fd");
605  }
606 #else
607  dir = opendir("/dev/fd");
608 #endif
609  if (dir != NULL) {
610  struct dirent *entry;
611  int dir_fd = dirfd(dir);
612 
613  while ((entry = readdir(dir)) != NULL) {
614  int lpc = atoi(entry->d_name);
615 
616  /* How could one of these entries be higher than max_fd, you ask?
617  * It isn't possible in normal operation, but when run under
618  * valgrind, valgrind can open high-numbered file descriptors for
619  * its own use that are higher than the process's soft limit.
620  * These will show up in the fd directory but aren't closable.
621  */
622  if ((lpc >= min_fd) && (lpc <= max_fd) && (lpc != dir_fd)) {
623  close(lpc);
624  }
625  }
626  closedir(dir);
627  return;
628  }
629 
630  /* If no fd directory is available, iterate over all possible descriptors.
631  * This is less efficient due to the overhead of many system calls.
632  */
633  for (int lpc = max_fd; lpc >= min_fd; lpc--) {
634  close(lpc);
635  }
636 }
rc
int rc
Definition: pcmk_fence.c:35
pcmk__real_path
int pcmk__real_path(const char *path, char **resolved_path)
Definition: io.c:104
pcmk__build_path
int pcmk__build_path(const char *path_c, mode_t mode)
Definition: io.c:45
pcmk_strerror
const char * pcmk_strerror(int rc)
Definition: results.c:58
CRM_CHECK
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:215
crm_notice
#define crm_notice(fmt, args...)
Definition: logging.h:349
crm_err
#define crm_err(fmt, args...)
Definition: logging.h:347
crm_trace
#define crm_trace(fmt, args...)
Definition: logging.h:353
pcmk_rc_unknown_format
@ pcmk_rc_unknown_format
Definition: results.h:136
strerror
char * strerror(int errnum)
pcmk__sync_directory
void pcmk__sync_directory(const char *name)
Definition: io.c:415
pcmk__write_sync
int pcmk__write_sync(int fd, const char *contents)
Definition: io.c:507
pcmk__read_series_sequence
int pcmk__read_series_sequence(const char *directory, const char *series, unsigned int *seq)
Definition: io.c:159
CRM_XS
#define CRM_XS
Definition: logging.h:54
pcmk_rc_str
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
Definition: results.c:420
pcmk__get_tmpdir
const char * pcmk__get_tmpdir()
Definition: io.c:560
crm_strdup_printf
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
crm_debug
#define crm_debug(fmt, args...)
Definition: logging.h:352
pcmk__chown_series_sequence
int pcmk__chown_series_sequence(const char *directory, const char *series, uid_t uid, gid_t gid)
Definition: io.c:257
pcmk__close_fds_in_child
void pcmk__close_fds_in_child(bool all)
Definition: io.c:578
crm_build_path
void crm_build_path(const char *path_c, mode_t mode)
Create a directory, including any parent directories needed.
Definition: io.c:81
CRM_DAEMON_GROUP
#define CRM_DAEMON_GROUP
Definition: config.h:29
target
const char * target
Definition: pcmk_fence.c:29
crm_perror
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
Definition: logging.h:298
pcmk__series_filename
char * pcmk__series_filename(const char *directory, const char *series, int sequence, bool bzip)
Definition: io.c:140
pcmk__write_series_sequence
void pcmk__write_series_sequence(const char *directory, const char *series, unsigned int sequence, int max)
Definition: io.c:207
CRM_ASSERT
#define CRM_ASSERT(expr)
Definition: results.h:42
pcmk_rc_ok
@ pcmk_rc_ok
Definition: results.h:142
pcmk__set_nonblocking
int pcmk__set_nonblocking(int fd)
Definition: io.c:537
pcmk__file_contents
int pcmk__file_contents(const char *filename, char **contents)
Definition: io.c:451
CRM_DAEMON_USER
#define CRM_DAEMON_USER
Definition: config.h:32
crm_internal.h
util.h
Utility functions.
pcmk__daemon_can_write
bool pcmk__daemon_can_write(const char *dir, const char *file)
Definition: io.c:347
name
char * name
Definition: pcmk_fence.c:31
crm.h
A dumping ground.