summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-03-14 16:44:48 +0800
committerin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-03-14 16:44:48 +0800
commit30aaf378e0410c941dd7d7043aac0505ef44e2e1 (patch)
treee89dac202ad128cf964d5782830546b17cd6640b /util
parentc0eff88a89de7c10ccfc23416098476e2d20a0c3 (diff)
downloadpttbbs-30aaf378e0410c941dd7d7043aac0505ef44e2e1.tar
pttbbs-30aaf378e0410c941dd7d7043aac0505ef44e2e1.tar.gz
pttbbs-30aaf378e0410c941dd7d7043aac0505ef44e2e1.tar.bz2
pttbbs-30aaf378e0410c941dd7d7043aac0505ef44e2e1.tar.lz
pttbbs-30aaf378e0410c941dd7d7043aac0505ef44e2e1.tar.xz
pttbbs-30aaf378e0410c941dd7d7043aac0505ef44e2e1.tar.zst
pttbbs-30aaf378e0410c941dd7d7043aac0505ef44e2e1.zip
use /proc and check permission
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@24 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'util')
-rw-r--r--util/bbsctl.c82
1 files changed, 52 insertions, 30 deletions
diff --git a/util/bbsctl.c b/util/bbsctl.c
index 62d00300..db158c77 100644
--- a/util/bbsctl.c
+++ b/util/bbsctl.c
@@ -5,16 +5,14 @@
#include <signal.h>
#include <sys/types.h>
#include <grp.h>
+#include <dirent.h>
+
#ifdef FreeBSD
#include <sys/syslimits.h>
- #define PS "/bin/ps"
- #define GREP "/usr/bin/grep"
#define SU "/usr/bin/su"
#endif
#ifdef Linux
#include <linux/limits.h>
- #define PS "/bin/ps"
- #define GREP "/bin/grep"
#define SU "/bin/su"
#endif
@@ -24,6 +22,29 @@ void usage(void)
exit(0);
}
+int HaveBBSADM(void)
+{
+ gid_t gids[NGROUPS_MAX];
+ int i, ngids;
+ struct group *gr;
+ ngids = getgroups(NGROUPS_MAX, gids);
+ if( (gr = getgrnam("bbsadm")) == NULL ){
+ puts("group bbsadm not found");
+ return 0;
+ }
+
+ for( i = 0 ; i < ngids ; ++i )
+ if( gr->gr_gid == gids[i] )
+ break;
+
+ if( i == ngids ){
+ puts("permission denied");
+ return 0;
+ }
+
+ return 1;
+}
+
void startbbs(void)
{
if( setuid(0) < 0 ){
@@ -46,43 +67,42 @@ void startbbs(void)
void stopbbs(void)
{
- char buf[1024];
- int pid;
- FILE *fp = popen(PS " -ax | " GREP " mbbsd | "
- GREP " listen", "r");
- while( fgets(buf, sizeof(buf), fp) != NULL ){
- sscanf(buf, "%d", &pid);
- printf("stopping %d\n", pid);
- kill(pid, 1);
+ DIR *dirp;
+ struct dirent *de;
+ FILE *fp;
+ char buf[512];
+ if( !(dirp = opendir("/proc")) ){
+ perror("open /proc");
+ exit(0);
+ }
+
+ while( (de = readdir(dirp)) ){
+ if( de->d_type & DT_DIR ){
+ sprintf(buf, "/proc/%s/cmdline", de->d_name);
+ if( (fp = fopen(buf, "r")) ){
+ if( fgets(buf, sizeof(buf), fp) != NULL ){
+ if( strstr(buf, "mbbsd") && strstr(buf, "listening") ){
+ kill(atoi(de->d_name), 1);
+ printf("stopping mbbsd at pid %5d\n", atoi(de->d_name));
+ }
+ }
+ fclose(fp);
+ }
+ }
}
+
+ closedir(dirp);
}
void restartbbs(void)
{
stopbbs();
+ sleep(1);
startbbs();
}
void bbsadm(void)
{
- gid_t gids[NGROUPS_MAX];
- int i, ngids;
- struct group *gr;
- ngids = getgroups(NGROUPS_MAX, gids);
- if( (gr = getgrnam("bbsadm")) == NULL ){
- puts("group bbsadm not found");
- return;
- }
-
- for( i = 0 ; i < ngids ; ++i )
- if( gr->gr_gid == gids[i] )
- break;
-
- if( i == ngids ){
- puts("permission denied");
- return;
- }
-
if( setuid(0) < 0 ){
perror("setuid(0)");
return;
@@ -105,6 +125,8 @@ int main(int argc, char **argv)
int i;
if( argc == 1 )
usage();
+ if( !HaveBBSADM() )
+ return 1;
for( i = 0 ; cmds[i].cmd != NULL ; ++i )
if( strcmp(cmds[i].cmd, argv[1]) == 0 ){
cmds[i].func();