Article View: alt.pl.fan.gopher
Article #15396Re: Dostałem...
From: Adam Wysocki
Date: Thu, 28 Aug 2003 04:48
Date: Thu, 28 Aug 2003 04:48
291 lines
7970 bytes
7970 bytes
28.08.03 puck@go2.pl napisał:
> > > > > > Tak :)
> > > > > Świetnie. Jeszcze jakieś wątpliwości, Kamil?
> > > > Pewnie ma ;>
> > > Zobaczymy.
> > Musimy? :)
>
> Mówisz o Plunku? :) Po co?
Nie ;)
A co do ~/.plan, serwer, który wyłożył apcoh.org (odpalony
z usera), to (na razie jest tylko tyle kodu, będzie więcej,
jak sprawdzę, dlaczego ten się forkuje w nieskończoność):
/*
* Copyright (C) 2003 Adam Wysocki <gophi@linux.net.pl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* *******************************************************************
*
* Personal Finger Daemon
*
* Latest version: http://www.gophi.apcoh.org/projekty/pfingerd.c
*
* Short, nice and fully customizable finger daemon.
*
* Config file (/etc/pfingerd.conf if not specified otherwise) consists
* of line-per-user entries and * for default entry. Each line consists
* of configuration options separated by ':'. These options are:
*
* username: I think it's clear :)
* want to be fingered: just say y or n.
* full name: full name of the user, or % for passwd entry.
* home directory: as above.
* shell: as above.
* phone: as above.
* logins: y or n (if y, show logins).
* mail: if y, show lastreceived and lastread mail - only for mbox.
* plan: y or n (if y, show planfile).
*
*/
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define CONFFILE "/etc/pfingerd.conf"
#define PIDFILE "/var/run/pfingerd.pid"
#define PORT 31330 /* just temp */
extern char *optarg;
extern int optind, opterr, optopt;
char *pname;
char wrote_pid = 0;
char *pidfile;
char do_daemonize (void)
{
FILE *fp;
pid_t forkret;
forkret = fork();
if (forkret < 0) { /* error */
fprintf (stderr, "%s: do_daemonize: fork: %s.\n", pname, strerror(errno));
return -1;
} else if (forkret > 0) /* we're parent */
_exit (0);
if (setsid() < 0) {
fprintf (stderr, "%s: do_daemonize: setsid: %s.\n", pname, strerror(errno));
return -1;
}
fp = fopen("/dev/null", "r+");
if (!fp) {
fprintf (stderr, "%s: do_daemonize: fopen: %s.\n", pname, strerror(errno));
return -1;
}
dup2 (fileno(fp), 0); /* stdin */
dup2 (fileno(fp), 0); /* stdout */
dup2 (fileno(fp), 0); /* stderr */
fclose (fp);
return 0;
}
static void sighnd_child (int signo)
{
pid_t pid;
int stat;
pid = 0;
while (pid > 0)
pid = waitpid(-1, &stat, WNOHANG);
}
static void sighnd_term (int signo)
{
if (wrote_pid)
unlink (pidfile);
exit (0);
}
static void sighnd_reload (int signo)
{
/* ;> */
}
void hook_signals (void)
{
signal (SIGCHLD, sighnd_child);
signal (SIGPIPE, sighnd_term);
signal (SIGTERM, sighnd_term);
signal (SIGQUIT, sighnd_term);
signal (SIGINT, sighnd_term);
signal (SIGHUP, sighnd_reload);
}
void write_pid (void)
{
FILE *fp;
fp = fopen(pidfile, "w");
if (!fp)
return;
fprintf (fp, "%u\n", getpid());
fclose (fp);
wrote_pid = 1;
}
int do_listen (int port)
{
int fd;
struct sockaddr_in addr;
int result;
fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd <= 0) {
fprintf (stderr, "%s: do_listen: socket: %s\n", pname, strerror(errno));
return -1;
}
fcntl (fd, F_SETFL, O_NONBLOCK);
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(port);
addr.sin_family = AF_INET;
result = bind(fd, (struct sockaddr *) &addr, sizeof(addr));
if (result < 0) {
fprintf (stderr, "%s: do_listen: bind: %s\n", pname, strerror(errno));
return -1;
}
result = listen(fd, 10);
if (result < 0) {
fprintf (stderr, "%s: do_listen: listen: %s\n", pname, strerror(errno));
return -1;
}
return fd;
}
void handle_request (int fd)
{
const char *str = "Działa?\r\n";
write (fd, str, sizeof(str));
}
int main (int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_in new_addr;
socklen_t addr_sz;
pid_t child_pid;
const char *options = "df:c:p:h";
char *conffile = CONFFILE;
int bindport = PORT;
char daemonize = 0;
char optdone = 0;
int optret;
char done = 0;
pname = argv[0];
while (!done) {
optret = getopt(argc, argv, options);
switch (optret) {
case ':':
fprintf (stderr, "%s: command line: missing parameter\n", argv[0]);
done = 3;
break;
case '?':
fprintf (stderr, "%s: command line: unknown option\n", argv[0]);
done = 3;
break;
case -1:
done = 1;
break;
case 'h':
printf ("Syntax: %s [options]\n", argv[0]);
printf (" Options:\n");
printf (" -h: fasthelp\n");
printf (" -d: daemonize\n");
printf (" -f pidfile: write pid to this file (only if daemonize enabled, default %s)\n", PIDFILE);
printf (" -c configfile: read config from this file (default %s)\n", CONFFILE);
printf (" -p bindport: bind to this port (default %u)\n", PORT);
done = 2;
break;
case 'd':
daemonize = 1;
break;
case 'f':
pidfile = optarg;
break;
case 'c':
conffile = optarg;
break;
case 'p':
bindport = atoi(optarg);
break;
}
}
if (done >= 2)
return -(done - 2);
if (daemonize)
printf ("Running in background.\n");
else
printf ("Running in foreground.\n");
printf ("Using pid file: %s.\n", pidfile);
printf ("Using config file: %s.\n", conffile);
printf ("Binding to port %u.\n", bindport);
if (daemonize)
if (do_daemonize())
return -3;
else
write_pid();
listenfd = do_listen(bindport);
addr_sz = sizeof(new_addr);
hook_signals();
while (!0) {
connfd = accept(listenfd, (struct sockaddr *) &new_addr, &addr_sz);
if (connfd < 0 && errno == EINTR)
continue;
child_pid = fork();
if (!child_pid) { /* we're a child */
close (listenfd);
handle_request (connfd);
}
close (connfd);
}
return 0;
}
--
Pozdrowienia * Adam Wysocki * gophi@studio.tpi.pl gophi@gadu-gadu.pl
GG 1234 * GSM +48505439030 * gophi@linux.net.pl * gophi@sexonline.pl
Technical Details
Message-ID:
<gophi.7879@gophi.to.najkochanszy.smerfinek.a.jak.nie.to.kicaj.net>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news2.icm.edu.pl!news.atcom.net.pl!apcoh.org!gophiReferences:
<bhi7m5$hc0$4@atlantis.news.tpi.pl> <PEI.08.0209@garfield.blade.net> <bhjira$gu2$6@atlantis.news.tpi.pl> <PEI.08.0218@garfield.blade.net> <bidtfc$juq$1@nemesis.news.tpi.pl> <PEI.08.033B@garfield.blade.net> <gophi.7777@gophi.to.najkochanszy.smerfinek.a.jak.nie.to.kicaj.net> <PEI.08.0344@garfield.blade.net> <gophi.7813@gophi.to.najkochanszy.smerfinek.a.jak.nie.to.kicaj.net> <PEI.08.034D@garfield.blade.net> <gophi.7857@gophi.to.najkochanszy.smerfinek.a.jak.nie.to.kicaj.net> <PEI.08.035B@garfield.blade.net>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news2.icm.edu.pl!news.atcom.net.pl!apcoh.org!gophi From: Adam Wysocki <gophi@apcoh.org> Newsgroups: alt.pl.fan.gopher,alt.pl.garfield Subject: Re: Dostałem... Date: Thu, 28 Aug 2003 06:48:36 +0200 Organization: ATCOM ISP http://www.atcom.pl Lines: 291 Message-ID: <gophi.7879@gophi.to.najkochanszy.smerfinek.a.jak.nie.to.kicaj.net> References: <bhi7m5$hc0$4@atlantis.news.tpi.pl> <PEI.08.0209@garfield.blade.net> <bhjira$gu2$6@atlantis.news.tpi.pl> <PEI.08.0218@garfield.blade.net> <bidtfc$juq$1@nemesis.news.tpi.pl> <PEI.08.033B@garfield.blade.net> <gophi.7777@gophi.to.najkochanszy.smerfinek.a.jak.nie.to.kicaj.net> <PEI.08.0344@garfield.blade.net> <gophi.7813@gophi.to.najkochanszy.smerfinek.a.jak.nie.to.kicaj.net> <PEI.08.034D@garfield.blade.net> <gophi.7857@gophi.to.najkochanszy.smerfinek.a.jak.nie.to.kicaj.net> <PEI.08.035B@garfield.blade.net> NNTP-Posting-Host: apcoh.org Mime-Version: 1.0 X-Trace: sklad.atcom.net.pl 1062046128 10157 217.197.167.123 (28 Aug 2003 04:48:48 GMT) X-Complaints-To: news@atcom.pl NNTP-Posting-Date: Thu, 28 Aug 2003 04:48:48 +0000 (UTC) X-Troll-Reply: puck X-Tree: ;> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit