]>
jfr.im git - solanum.git/blob - tools/viconf.c
4 * $Id: viconf.c 6 2005-09-10 01:02:21Z nenolod $
16 #ifndef PROGRAM_PREFIX
17 #define PROGRAM_PREFIX ""
20 /* wait.h is in /include on solaris, likely on other SYSV machines as well
21 * but wait.h is normally in /include/sys on BSD boxen,
22 * probably we should have an #ifdef SYSV?
32 static int LockedFile(const char *filename
);
33 static char lockpath
[PATH_MAX
+ 1];
36 int main(int argc
, char *argv
[])
38 const char *ed
, *p
, *filename
= CPATH
;
40 if( chdir(DPATH
) < 0 )
42 fprintf(stderr
,"Cannot chdir to %s\n", DPATH
);
46 if((p
= strrchr(argv
[0], '/')) == NULL
)
51 if(strcmp(p
, PROGRAM_PREFIX
"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());