redmine

Fixed a bug "permission denied" with "--pid-file".

Clsync uses effective pid of "nobody" by default. In turn there's
no permission to create new files in /var/run on some systems under
the "nobody". So this patch uses "root" to create and fchown() the
file.

    Debug3 (pid: 19860; thread: 0x7f606105db40): main(): Trying to drop effective gid to 65533
    Debug3 (pid: 19860; thread: 0x7f606105db40): main(): Trying to drop effective uid to 65534
    Debug1 (pid: 19860; thread: 0x7f606105db40): main(): / [/] (0xf7eed0) -> rsync://********@**********/******* [***] (0xf7ba30)
    Warning (pid: 19860; thread: 0x7f606105db40): main(): Directory "/dev/shm/clsync" doesn't exist. Creating it.
    Debug2 (pid: 19860; thread: 0x7f606105db40): main(): Trying to open the pidfile "/var/run/clsync-backup.pid"
    Error (pid: 19860; thread: 0x7f606105db40): main(): Cannot open file "/var/run/clsync***.pid" to write a pid there (13: Permission denied)
    Debug3 (pid: 19860; thread: 0x7f606105db40): main(): Current errno is 13.
    Error (pid: 19860; thread: 0x7f606105db40): main(): Cannot unlink pidfile "/var/run/clsync***.pid" (2: No such file or directory)
Showing 1 changed file with 36 additions and 7 deletions
... ... @@ -2962,12 +2962,35 @@ int main(int _argc, char *_argv[]) {
if (ctx_p->pidfile != NULL) {
debug(2, "Trying to open the pidfile \"%s\"", ctx_p->pidfile);
pid_t pid = getpid();
FILE *pidfile = fopen(ctx_p->pidfile, "w");
if (pidfile == NULL) {
error("Cannot open file \"%s\" to write a pid there",
ctx_p->pidfile);
ret = errno;
} else {
// If error
if (errno == EACCES) {
int fd;
uid_t euid = geteuid();
gid_t egid = getegid();
debug(1, "Don't have permissions to open file \"%s\". Trying seteuid(0)+open()+fchown()+close()+seteuid(%i)", ctx_p->pidfile, euid);
errno = 0;
if (!errno) SAFE ( seteuid(0), ret = errno );
if (!errno) SAFE ( (fd = open(ctx_p->pidfile, O_CREAT|O_WRONLY, 0644)) == -1, ret = errno );
if (!errno) SAFE ( fchown(fd, euid, egid), ret = errno );
if (!errno) SAFE ( close(fd), ret = errno );
if (!errno) SAFE ( seteuid(euid), ret = errno );
pidfile = fopen(ctx_p->pidfile, "w");
}
if (pidfile == NULL) {
error("Cannot open file \"%s\" to write a pid there",
ctx_p->pidfile);
ret = errno;
}
}
if (pidfile != NULL) {
if (fprintf(pidfile, "%u", pid) < 0) {
error("Cannot write pid into file \"%s\"",
ctx_p->pidfile);
... ... @@ -2986,9 +3009,15 @@ int main(int _argc, char *_argv[]) {
if (ctx_p->pidfile != NULL) {
if (unlink(ctx_p->pidfile)) {
error("Cannot unlink pidfile \"%s\"",
ctx_p->pidfile);
ret = errno;
FILE *pidfile;
debug(1, "Cannot unlink pidfile \"%s\": %s. Just truncating the file.",
ctx_p->pidfile, strerror(errno));
SAFE ( (pidfile = fopen(ctx_p->pidfile, "w")) == NULL, ret = errno );
if (pidfile != NULL)
fclose(pidfile);
}
}
... ...