-#ifndef DEBUG
+#ifndef bmalloc
void *bmalloc(size_t size)
{
void *buf;
+#ifdef SMARTALLOC
+ buf = sm_malloc(file, line, size);
+#else
buf = malloc(size);
+#endif
if (buf == NULL) {
berrno be;
Emsg1(M_ABORT, 0, _("Out of memory: ERR=%s\n"), be.strerror());
}
+void bfree(void *buf)
+{
+#ifdef SMARTALLOC
+ sm_free(__FILE__, __LINE__, buf);
+#else
+ free(buf);
+#endif
+}
+
+
+
+
void *brealloc (void *buf, size_t size)
{
buf = realloc(buf, size);
0
};
-#ifdef HAVE_WIN32
-#undef open
-#undef read
-#undef write
-#undef lseek
-#undef close
-#undef O_BINARY
-#define open _open
-#define read _read
-#define write _write
-#define lseek _lseeki64
-#define close _close
-#define O_BINARY _O_BINARY
-#endif
-
/*
* Open and read the state file for the daemon
*/
struct passwd *passw = NULL;
struct group *group = NULL;
gid_t gid;
+ uid_t uid;
+ char username[1000];
Dmsg2(900, "uname=%s gname=%s\n", uname?uname:"NONE", gname?gname:"NONE");
if (!uname && !gname) {
uname = passw->pw_name;
}
}
+ /* Any OS uname pointer may get overwritten, so save name, uid, and gid */
+ bstrncpy(username, uname, sizeof(username));
+ uid = passw->pw_uid;
+ gid = passw->pw_gid;
if (gname) {
if ((group = getgrnam(gname)) == NULL) {
berrno be;
be.strerror());
}
gid = group->gr_gid;
- } else {
- gid = passw->pw_gid;
}
- if (initgroups(passw->pw_name, passw->pw_gid)) {
+ if (initgroups(username, gid)) {
berrno be;
if (gname) {
Emsg3(M_ERROR_TERM, 0, _("Could not initgroups for group=%s, userid=%s: ERR=%s\n"),
- gname, uname, be.strerror());
+ gname, username, be.strerror());
} else {
Emsg2(M_ERROR_TERM, 0, _("Could not initgroups for userid=%s: ERR=%s\n"),
- uname, be.strerror());
+ username, be.strerror());
}
}
if (gname) {
- if (setgid(group->gr_gid)) {
+ if (setgid(gid)) {
berrno be;
Emsg2(M_ERROR_TERM, 0, _("Could not set group=%s: ERR=%s\n"), gname,
be.strerror());
}
}
- if (setuid(passw->pw_uid)) {
+ if (setuid(uid)) {
berrno be;
- Emsg1(M_ERROR_TERM, 0, _("Could not set specified userid: %s\n"), uname);
+ Emsg1(M_ERROR_TERM, 0, _("Could not set specified userid: %s\n"), username);
}
#endif
}
do {
errno = 0;
ch = fgetc(fd);
- } while (ch == -1 && (errno == EINTR || errno == EAGAIN));
- if (ch == -1) {
+ } while (ch == EOF && ferror(fd) && (errno == EINTR || errno == EAGAIN));
+ if (ch == EOF) {
if (i == 0) {
return NULL;
} else {
*p = 0;
if (ch == '\r') { /* Support for Mac/Windows file format */
ch = fgetc(fd);
- if (ch == '\n') { /* Windows (\r\n) */
- *p++ = ch;
- *p = 0;
- }
- else { /* Mac (\r only) */
+ if (ch != '\n') { /* Mac (\r only) */
(void)ungetc(ch, fd); /* Push next character back to fd */
}
+ p[-1] = '\n';
break;
}
if (ch == '\n') {
{
Mmsg(name, "%s/%s.%s.%d.tmp", working_directory, my_name, what, Id);
}
+
+char *escape_filename(const char *file_path)
+{
+ if (file_path == NULL || strpbrk(file_path, "\"\\") == NULL) {
+ return NULL;
+ }
+
+ char *escaped_path = (char *)bmalloc(2 * (strlen(file_path) + 1));
+ char *cur_char = escaped_path;
+
+ while (*file_path) {
+ if (*file_path == '\\' || *file_path == '"') {
+ *cur_char++ = '\\';
+ }
+
+ *cur_char++ = *file_path++;
+ }
+
+ *cur_char = '\0';
+
+ return escaped_path;
+}
+
+