16 #include <sys/param.h>
17 #include <sys/types.h>
19 #include <sys/resource.h>
44 int offset = 1, len = 0;
45 char *path = strdup(path_c);
48 for (len = strlen(path); offset < len; offset++) {
49 if (path[offset] ==
'/') {
51 if (mkdir(path, mode) < 0 && errno != EEXIST) {
52 crm_perror(LOG_ERR,
"Could not create directory '%s'", path);
58 if (mkdir(path, mode) < 0 && errno != EEXIST) {
59 crm_perror(LOG_ERR,
"Could not create directory '%s'", path);
80 const char *ext =
"raw";
82 CRM_CHECK(directory != NULL,
return NULL);
103 FILE *file_strm = NULL;
104 int start = 0, length = 0, read_len = 0;
105 char *series_file = NULL;
113 file_strm = fopen(series_file,
"r");
114 if (file_strm == NULL) {
115 crm_debug(
"Series file %s does not exist", series_file);
121 start = ftell(file_strm);
122 fseek(file_strm, 0L, SEEK_END);
123 length = ftell(file_strm);
124 fseek(file_strm, 0L, start);
130 crm_info(
"%s was not valid", series_file);
135 crm_trace(
"Reading %d bytes from file", length);
136 buffer = calloc(1, (length + 1));
137 read_len = fread(buffer, 1, length, file_strm);
138 if (read_len != length) {
139 crm_err(
"Calculated and read bytes differ: %d vs. %d", length, read_len);
148 crm_trace(
"Found %d in %s", seq, series_file);
170 FILE *file_strm = NULL;
171 char *series_file = NULL;
179 if (max > 0 && sequence >= max) {
184 file_strm = fopen(series_file,
"w");
185 if (file_strm != NULL) {
186 rc = fprintf(file_strm,
"%d", sequence);
188 crm_perror(LOG_ERR,
"Cannot write to series file %s", series_file);
192 crm_err(
"Cannot open series file %s for writing", series_file);
195 if (file_strm != NULL) {
200 crm_trace(
"Wrote %d to %s", sequence, series_file);
218 char *series_file = NULL;
221 CRM_CHECK((directory != NULL) && (series != NULL), errno = EINVAL;
return -1);
224 CRM_CHECK(series_file != NULL,
return -1);
226 rc = chown(series_file, uid, gid);
232 pcmk__daemon_user_can_write(
const char *target_name,
struct stat *target_stat)
234 struct passwd *sys_user = NULL;
238 if (sys_user == NULL) {
243 if (target_stat->st_uid != sys_user->pw_uid) {
246 target_stat->st_uid);
249 if ((target_stat->st_mode & (S_IRUSR | S_IWUSR)) == 0) {
250 crm_notice(
"%s is not readable and writable by user %s "
253 (
unsigned long) target_stat->st_mode);
260 pcmk__daemon_group_can_write(
const char *target_name,
struct stat *target_stat)
262 struct group *sys_grp = NULL;
266 if (sys_grp == NULL) {
272 if (target_stat->st_gid != sys_grp->gr_gid) {
275 sys_grp->gr_gid, target_stat->st_gid);
279 if ((target_stat->st_mode & (S_IRGRP | S_IWGRP)) == 0) {
280 crm_notice(
"%s is not readable and writable by group %s "
283 (
unsigned long) target_stat->st_mode);
308 char *full_file = NULL;
309 const char *target = NULL;
316 full_file = crm_concat(dir, file,
'/');
319 s_res = stat(full_file, &buf);
326 }
else if (S_ISREG(buf.st_mode) == FALSE) {
328 target, (
unsigned long) buf.st_mode);
335 if (target == NULL) {
337 s_res = stat(dir, &buf);
342 }
else if (S_ISDIR(buf.st_mode) == FALSE) {
344 dir, (
unsigned long) buf.st_mode);
349 if (!pcmk__daemon_user_can_write(target, &buf)
350 && !pcmk__daemon_group_can_write(target, &buf)) {
352 crm_err(
"%s must be owned and writable by either user %s or group %s "
355 (
unsigned long) buf.st_mode);
377 directory = opendir(name);
378 if (directory == NULL) {
379 crm_perror(LOG_ERR,
"Could not open %s for syncing", name);
383 fd = dirfd(directory);
385 crm_perror(LOG_ERR,
"Could not obtain file descriptor for %s", name);
390 crm_perror(LOG_ERR,
"Could not sync %s", name);
392 if (closedir(directory) < 0) {
393 crm_perror(LOG_ERR,
"Could not close %s after fsync", name);
411 char *contents = NULL;
413 int length, read_len;
417 fp = fopen(filename,
"r");
422 fseek(fp, 0L, SEEK_END);
426 contents = calloc(length + 1,
sizeof(
char));
427 if (contents == NULL) {
432 crm_trace(
"Reading %d bytes from %s", length, filename);
434 read_len = fread(contents, 1, length, fp);
435 if (read_len != length) {
458 FILE *fp = fdopen(fd,
"w");
463 if ((contents != NULL) && (fprintf(fp,
"%s", contents) < 0)) {
466 if (fflush(fp) != 0) {
469 if (fsync(fileno(fp)) < 0) {
487 int flag = fcntl(fd, F_GETFL);
492 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
501 const char *dir = getenv(
"TMPDIR");
503 return (dir && (*dir ==
'/'))? dir :
"/tmp";
522 int min_fd = (all? 0 : (STDERR_FILENO + 1));
527 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
528 max_fd = rlim.rlim_cur - 1;
530 long conf_max = sysconf(_SC_OPEN_MAX);
532 max_fd = (conf_max > 0)? conf_max : 1024;
541 dir = opendir(
"/proc/self/fd");
543 dir = opendir(
"/dev/fd");
546 dir = opendir(
"/dev/fd");
549 struct dirent *entry;
550 int dir_fd = dirfd(dir);
552 while ((entry = readdir(dir)) != NULL) {
553 int lpc = atoi(entry->d_name);
561 if ((lpc >= min_fd) && (lpc <= max_fd) && (lpc != dir_fd)) {
572 for (
int lpc = max_fd; lpc >= min_fd; lpc--) {