4 * $Id: viconf.c 13238 2002-11-29 17:28:30Z leeh $
17 /* wait.h is in /include on solaris, likely on other SYSV machines as well
18 * but wait.h is normally in /include/sys on BSD boxen,
19 * probably we should have an #ifdef SYSV?
29 static int LockedFile(const char *filename
);
30 static char lockpath
[PATH_MAX
+ 1];
33 int main(int argc
, char *argv
[])
35 const char *ed
, *p
, *filename
= CPATH
;
37 if( chdir(DPATH
) < 0 )
39 fprintf(stderr
,"Cannot chdir to %s\n", DPATH
);
43 if((p
= strrchr(argv
[0], '/')) == NULL
)
48 if(strcmp(p
, "viklines") == 0)
52 if(strcmp(p
, "vimotd") == 0)
55 if(LockedFile(filename
))
57 fprintf(stderr
,"Can't lock %s\n", filename
);
65 fprintf(stderr
, "error forking, %d\n", errno
);
68 if((ed
= getenv("EDITOR")) == NULL
)
70 execlp(ed
, ed
, filename
, NULL
);
71 fprintf(stderr
, "error running editor, %d\n", errno
);
82 * LockedFile() (copied from m_kline.c in ircd)
83 * Determine if 'filename' is currently locked. If it is locked,
84 * there should be a filename.lock file which contains the current
85 * pid of the editing process. Make sure the pid is valid before
89 * -1 if couldn't unlock
90 * 0 if was able to lock
96 LockedFile(const char *filename
)
108 sprintf(lockpath
, "%s.lock", filename
);
110 if ((fileptr
= fopen(lockpath
, "r")) != NULL
)
112 if (fgets(buffer
, sizeof(buffer
) - 1, fileptr
))
115 * If it is a valid lockfile, 'buffer' should now
116 * contain the pid number of the editing process.
117 * Send the pid a SIGCHLD to see if it is a valid
118 * pid - it could be a remnant left over from a
119 * crashed editor or system reboot etc.
122 killret
= kill(atoi(buffer
), SIGCHLD
);
130 * killret must be -1, which indicates an error (most
131 * likely ESRCH - No such process), so it is ok to
132 * proceed writing klines.
139 * Delete the outdated lock file
143 /* create exclusive lock */
144 if((fd
= open(lockpath
, O_WRONLY
|O_CREAT
|O_EXCL
, 0666)) < 0)
146 fprintf(stderr
, "ircd config file locked\n");
150 fileptr
= fdopen(fd
,"w");
151 fprintf(fileptr
,"%d\n",(int)getpid());