]>
jfr.im git - irc/rqf/shadowircd.git/blob - tools/viconf.c
e18fc359b39eaf39f33de968aed45a0893cef01a
16 /* wait.h is in /include on solaris, likely on other SYSV machines as well
17 * but wait.h is normally in /include/sys on BSD boxen,
18 * probably we should have an #ifdef SYSV?
28 static int LockedFile(const char *filename
);
29 static char lockpath
[PATH_MAX
+ 1];
32 int main(int argc
, char *argv
[])
34 const char *ed
, *p
, *filename
= CPATH
;
36 if( chdir(DPATH
) < 0 )
38 fprintf(stderr
,"Cannot chdir to %s\n", DPATH
);
42 if((p
= strrchr(argv
[0], '/')) == NULL
)
47 if(strcmp(p
, "viklines") == 0)
51 if(strcmp(p
, "vimotd") == 0)
54 if(LockedFile(filename
))
56 fprintf(stderr
,"Can't lock %s\n", filename
);
64 fprintf(stderr
, "error forking, %d\n", errno
);
67 if((ed
= getenv("EDITOR")) == NULL
)
69 execlp(ed
, ed
, filename
, NULL
);
70 fprintf(stderr
, "error running editor, %d\n", errno
);
81 * LockedFile() (copied from m_kline.c in ircd)
82 * Determine if 'filename' is currently locked. If it is locked,
83 * there should be a filename.lock file which contains the current
84 * pid of the editing process. Make sure the pid is valid before
88 * -1 if couldn't unlock
89 * 0 if was able to lock
95 LockedFile(const char *filename
)
107 sprintf(lockpath
, "%s.lock", filename
);
109 if ((fileptr
= fopen(lockpath
, "r")) != NULL
)
111 if (fgets(buffer
, sizeof(buffer
) - 1, fileptr
))
114 * If it is a valid lockfile, 'buffer' should now
115 * contain the pid number of the editing process.
116 * Send the pid a SIGCHLD to see if it is a valid
117 * pid - it could be a remnant left over from a
118 * crashed editor or system reboot etc.
121 killret
= kill(atoi(buffer
), SIGCHLD
);
129 * killret must be -1, which indicates an error (most
130 * likely ESRCH - No such process), so it is ok to
131 * proceed writing klines.
138 * Delete the outdated lock file
142 /* create exclusive lock */
143 if((fd
= open(lockpath
, O_WRONLY
|O_CREAT
|O_EXCL
, 0666)) < 0)
145 fprintf(stderr
, "ircd config file locked\n");
149 fileptr
= fdopen(fd
,"w");
150 fprintf(fileptr
,"%d\n",(int)getpid());