16 #include <sys/param.h>
17 #include <sys/types.h>
19 #include <sys/resource.h>
47 int offset = 1, len = 0;
49 char *path = strdup(path_c);
54 for (len = strlen(path); offset < len; offset++) {
55 if (path[offset] ==
'/') {
57 if ((mkdir(path, mode) < 0) && (errno != EEXIST)) {
64 if ((mkdir(path, mode) < 0) && (errno != EEXIST)) {
86 crm_err(
"Could not create directory '%s': %s",
106 CRM_CHECK((path != NULL) && (resolved_path != NULL),
return EINVAL);
108 #if _POSIX_VERSION >= 200809L
110 *resolved_path = realpath(path, NULL);
111 return (*resolved_path == NULL)? errno :
pcmk_rc_ok;
113 #elif defined(PATH_MAX)
116 *resolved_path = malloc(PATH_MAX);
117 if ((*resolved_path == NULL) || (realpath(path, *resolved_path) == NULL)) {
122 *resolved_path = NULL;
143 CRM_ASSERT((directory != NULL) && (series != NULL));
145 (bzip?
"bz2" :
"raw"));
164 char *series_file = NULL;
166 if ((directory == NULL) || (series == NULL) || (seq == NULL)) {
171 fp = fopen(series_file,
"r");
174 crm_debug(
"Could not open series file %s: %s",
180 if (fscanf(fp,
"%u", seq) != 1) {
182 crm_debug(
"Could not read sequence number from series file %s: %s",
189 crm_trace(
"Found last sequence number %u in series file %s",
208 unsigned int sequence,
int max)
211 FILE *file_strm = NULL;
212 char *series_file = NULL;
220 if (max > 0 && sequence >= max) {
225 file_strm = fopen(series_file,
"w");
226 if (file_strm != NULL) {
227 rc = fprintf(file_strm,
"%u", sequence);
229 crm_perror(LOG_ERR,
"Cannot write to series file %s", series_file);
233 crm_err(
"Cannot open series file %s for writing", series_file);
236 if (file_strm != NULL) {
241 crm_trace(
"Wrote %d to %s", sequence, series_file);
258 uid_t uid, gid_t gid)
260 char *series_file = NULL;
263 if ((directory == NULL) || (series == NULL)) {
267 if (chown(series_file, uid, gid) < 0) {
275 pcmk__daemon_user_can_write(
const char *target_name,
struct stat *target_stat)
277 struct passwd *sys_user = NULL;
281 if (sys_user == NULL) {
286 if (target_stat->st_uid != sys_user->pw_uid) {
289 target_stat->st_uid);
292 if ((target_stat->st_mode & (S_IRUSR | S_IWUSR)) == 0) {
293 crm_notice(
"%s is not readable and writable by user %s "
296 (
unsigned long) target_stat->st_mode);
303 pcmk__daemon_group_can_write(
const char *target_name,
struct stat *target_stat)
305 struct group *sys_grp = NULL;
309 if (sys_grp == NULL) {
315 if (target_stat->st_gid != sys_grp->gr_gid) {
318 sys_grp->gr_gid, target_stat->st_gid);
322 if ((target_stat->st_mode & (S_IRGRP | S_IWGRP)) == 0) {
323 crm_notice(
"%s is not readable and writable by group %s "
326 (
unsigned long) target_stat->st_mode);
351 char *full_file = NULL;
352 const char *
target = NULL;
362 s_res = stat(full_file, &buf);
369 }
else if (S_ISREG(buf.st_mode) == FALSE) {
371 target, (
unsigned long) buf.st_mode);
380 s_res = stat(dir, &buf);
385 }
else if (S_ISDIR(buf.st_mode) == FALSE) {
387 dir, (
unsigned long) buf.st_mode);
392 if (!pcmk__daemon_user_can_write(
target, &buf)
393 && !pcmk__daemon_group_can_write(
target, &buf)) {
395 crm_err(
"%s must be owned and writable by either user %s or group %s "
398 (
unsigned long) buf.st_mode);
420 directory = opendir(
name);
421 if (directory == NULL) {
426 fd = dirfd(directory);
428 crm_perror(LOG_ERR,
"Could not obtain file descriptor for %s",
name);
435 if (closedir(directory) < 0) {
454 int length, read_len;
457 if ((filename == NULL) || (contents == NULL)) {
461 fp = fopen(filename,
"r");
462 if ((fp == NULL) || (fseek(fp, 0L, SEEK_END) < 0)) {
476 *contents = calloc(length + 1,
sizeof(
char));
477 if (*contents == NULL) {
482 read_len = fread(*contents, 1, length, fp);
483 if (read_len != length) {
510 FILE *fp = fdopen(fd,
"w");
515 if ((contents != NULL) && (fprintf(fp,
"%s", contents) < 0)) {
518 if (fflush(fp) != 0) {
521 if (fsync(fileno(fp)) < 0) {
539 int flag = fcntl(fd, F_GETFL);
544 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
562 const char *dir = getenv(
"TMPDIR");
564 return (dir && (*dir ==
'/'))? dir :
"/tmp";
583 int min_fd = (all? 0 : (STDERR_FILENO + 1));
588 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
589 max_fd = rlim.rlim_cur - 1;
591 long conf_max = sysconf(_SC_OPEN_MAX);
593 max_fd = (conf_max > 0)? conf_max : 1024;
602 dir = opendir(
"/proc/self/fd");
604 dir = opendir(
"/dev/fd");
607 dir = opendir(
"/dev/fd");
610 struct dirent *entry;
611 int dir_fd = dirfd(dir);
613 while ((entry = readdir(dir)) != NULL) {
614 int lpc = atoi(entry->d_name);
622 if ((lpc >= min_fd) && (lpc <= max_fd) && (lpc != dir_fd)) {
633 for (
int lpc = max_fd; lpc >= min_fd; lpc--) {