]>
jfr.im git - solanum.git/blob - tools/viconf.c
4 * $Id: viconf.c 6 2005-09-10 01:02:21Z nenolod $
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
, "vimotd") == 0)
51 if(LockedFile(filename
))
53 fprintf(stderr
,"Can't lock %s\n", filename
);
61 fprintf(stderr
, "error forking, %d\n", errno
);
64 if((ed
= getenv("EDITOR")) == NULL
)
66 execlp(ed
, ed
, filename
, NULL
);
67 fprintf(stderr
, "error running editor, %d\n", errno
);
78 * LockedFile() (copied from m_kline.c in ircd)
79 * Determine if 'filename' is currently locked. If it is locked,
80 * there should be a filename.lock file which contains the current
81 * pid of the editing process. Make sure the pid is valid before
85 * -1 if couldn't unlock
86 * 0 if was able to lock
92 LockedFile(const char *filename
)
104 sprintf(lockpath
, "%s.lock", filename
);
106 if ((fileptr
= fopen(lockpath
, "r")) != NULL
)
108 if (fgets(buffer
, sizeof(buffer
) - 1, fileptr
))
111 * If it is a valid lockfile, 'buffer' should now
112 * contain the pid number of the editing process.
113 * Send the pid a SIGCHLD to see if it is a valid
114 * pid - it could be a remnant left over from a
115 * crashed editor or system reboot etc.
118 killret
= kill(atoi(buffer
), SIGCHLD
);
126 * killret must be -1, which indicates an error (most
127 * likely ESRCH - No such process), so it is ok to
128 * proceed writing klines.
135 * Delete the outdated lock file
139 /* create exclusive lock */
140 if((fd
= open(lockpath
, O_WRONLY
|O_CREAT
|O_EXCL
, 0666)) < 0)
142 fprintf(stderr
, "ircd config file locked\n");
146 fileptr
= fdopen(fd
,"w");
147 fprintf(fileptr
,"%d\n",(int)getpid());