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[]) { @@ -2962,12 +2962,35 @@ int main(int _argc, char *_argv[]) {
2962 if (ctx_p->pidfile != NULL) { 2962 if (ctx_p->pidfile != NULL) {
2963 debug(2, "Trying to open the pidfile \"%s\"", ctx_p->pidfile); 2963 debug(2, "Trying to open the pidfile \"%s\"", ctx_p->pidfile);
2964 pid_t pid = getpid(); 2964 pid_t pid = getpid();
  2965 +
2965 FILE *pidfile = fopen(ctx_p->pidfile, "w"); 2966 FILE *pidfile = fopen(ctx_p->pidfile, "w");
2966 if (pidfile == NULL) { 2967 if (pidfile == NULL) {
2967 - error("Cannot open file \"%s\" to write a pid there", 2968 + // If error
2968 - ctx_p->pidfile); 2969 + if (errno == EACCES) {
2969 - ret = errno; 2970 + int fd;
2970 - } else { 2971 + uid_t euid = geteuid();
  2972 + gid_t egid = getegid();
  2973 +
  2974 + debug(1, "Don't have permissions to open file \"%s\". Trying seteuid(0)+open()+fchown()+close()+seteuid(%i)", ctx_p->pidfile, euid);
  2975 +
  2976 + errno = 0;
  2977 + if (!errno) SAFE ( seteuid(0), ret = errno );
  2978 + if (!errno) SAFE ( (fd = open(ctx_p->pidfile, O_CREAT|O_WRONLY, 0644)) == -1, ret = errno );
  2979 + if (!errno) SAFE ( fchown(fd, euid, egid), ret = errno );
  2980 + if (!errno) SAFE ( close(fd), ret = errno );
  2981 + if (!errno) SAFE ( seteuid(euid), ret = errno );
  2982 +
  2983 + pidfile = fopen(ctx_p->pidfile, "w");
  2984 + }
  2985 +
  2986 + if (pidfile == NULL) {
  2987 + error("Cannot open file \"%s\" to write a pid there",
  2988 + ctx_p->pidfile);
  2989 + ret = errno;
  2990 + }
  2991 + }
  2992 +
  2993 + if (pidfile != NULL) {
2971 if (fprintf(pidfile, "%u", pid) < 0) { 2994 if (fprintf(pidfile, "%u", pid) < 0) {
2972 error("Cannot write pid into file \"%s\"", 2995 error("Cannot write pid into file \"%s\"",
2973 ctx_p->pidfile); 2996 ctx_p->pidfile);
@@ -2986,9 +3009,15 @@ int main(int _argc, char *_argv[]) { @@ -2986,9 +3009,15 @@ int main(int _argc, char *_argv[]) {
2986 3009
2987 if (ctx_p->pidfile != NULL) { 3010 if (ctx_p->pidfile != NULL) {
2988 if (unlink(ctx_p->pidfile)) { 3011 if (unlink(ctx_p->pidfile)) {
2989 - error("Cannot unlink pidfile \"%s\"", 3012 + FILE *pidfile;
2990 - ctx_p->pidfile); 3013 +
2991 - ret = errno; 3014 + debug(1, "Cannot unlink pidfile \"%s\": %s. Just truncating the file.",
  3015 + ctx_p->pidfile, strerror(errno));
  3016 +
  3017 + SAFE ( (pidfile = fopen(ctx_p->pidfile, "w")) == NULL, ret = errno );
  3018 +
  3019 + if (pidfile != NULL)
  3020 + fclose(pidfile);
2992 } 3021 }
2993 } 3022 }
2994 3023