]>
jfr.im git - irc/rqf/shadowircd.git/blob - tools/viconf.c
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
, "vimotd") == 0)
50 if(LockedFile(filename
))
52 fprintf(stderr
,"Can't lock %s\n", filename
);
60 fprintf(stderr
, "error forking, %d\n", errno
);
63 if((ed
= getenv("EDITOR")) == NULL
)
65 execlp(ed
, ed
, filename
, NULL
);
66 fprintf(stderr
, "error running editor, %d\n", errno
);
77 * LockedFile() (copied from m_kline.c in ircd)
78 * Determine if 'filename' is currently locked. If it is locked,
79 * there should be a filename.lock file which contains the current
80 * pid of the editing process. Make sure the pid is valid before
84 * -1 if couldn't unlock
85 * 0 if was able to lock
91 LockedFile(const char *filename
)
103 sprintf(lockpath
, "%s.lock", filename
);
105 if ((fileptr
= fopen(lockpath
, "r")) != NULL
)
107 if (fgets(buffer
, sizeof(buffer
) - 1, fileptr
))
110 * If it is a valid lockfile, 'buffer' should now
111 * contain the pid number of the editing process.
112 * Send the pid a SIGCHLD to see if it is a valid
113 * pid - it could be a remnant left over from a
114 * crashed editor or system reboot etc.
117 killret
= kill(atoi(buffer
), SIGCHLD
);
125 * killret must be -1, which indicates an error (most
126 * likely ESRCH - No such process), so it is ok to
127 * proceed writing klines.
134 * Delete the outdated lock file
138 /* create exclusive lock */
139 if((fd
= open(lockpath
, O_WRONLY
|O_CREAT
|O_EXCL
, 0666)) < 0)
141 fprintf(stderr
, "ircd config file locked\n");
145 fileptr
= fdopen(fd
,"w");
146 fprintf(fileptr
,"%d\n",(int)getpid());