1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* User Process PPP
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan, Inc. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
1998-05-23 22:24:50 +00:00
|
|
|
* $Id: main.c,v 1.123 1998/05/21 21:46:40 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
*/
|
1998-05-21 21:49:08 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1998-05-21 21:49:08 +00:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <net/if_tun.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <fcntl.h>
|
1995-10-08 14:57:32 +00:00
|
|
|
#include <paths.h>
|
1997-02-25 14:05:17 +00:00
|
|
|
#include <signal.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
1997-11-09 06:22:49 +00:00
|
|
|
#include "id.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "timer.h"
|
|
|
|
#include "fsm.h"
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "lqr.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "hdlc.h"
|
|
|
|
#include "lcp.h"
|
1997-12-03 10:23:54 +00:00
|
|
|
#include "ccp.h"
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "iplist.h"
|
|
|
|
#include "throughput.h"
|
|
|
|
#include "slcompress.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "ipcp.h"
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "filter.h"
|
|
|
|
#include "descriptor.h"
|
|
|
|
#include "link.h"
|
|
|
|
#include "mp.h"
|
|
|
|
#include "bundle.h"
|
1997-05-26 00:44:10 +00:00
|
|
|
#include "loadalias.h"
|
1995-02-26 12:18:08 +00:00
|
|
|
#include "auth.h"
|
1996-01-11 17:48:59 +00:00
|
|
|
#include "systems.h"
|
|
|
|
#include "ip.h"
|
1997-03-13 12:45:35 +00:00
|
|
|
#include "sig.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "main.h"
|
1997-11-16 22:15:11 +00:00
|
|
|
#include "tun.h"
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "server.h"
|
|
|
|
#include "prompt.h"
|
|
|
|
#include "chat.h"
|
|
|
|
#include "chap.h"
|
|
|
|
#include "datalink.h"
|
1995-02-26 12:18:08 +00:00
|
|
|
|
|
|
|
#ifndef O_NONBLOCK
|
|
|
|
#ifdef O_NDELAY
|
|
|
|
#define O_NONBLOCK O_NDELAY
|
|
|
|
#endif
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-05-10 03:39:57 +00:00
|
|
|
static char pid_filename[MAXPATHLEN];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
static void DoLoop(struct bundle *, struct prompt *);
|
1997-10-26 01:04:02 +00:00
|
|
|
static void TerminalStop(int);
|
1997-11-22 03:37:54 +00:00
|
|
|
static const char *ex_desc(int);
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
static struct bundle *SignalBundle;
|
|
|
|
static struct prompt *SignalPrompt;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
void
|
1998-05-21 21:49:08 +00:00
|
|
|
Cleanup(int excode)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
SignalBundle->CleaningUp = 1;
|
|
|
|
if (bundle_Phase(SignalBundle) != PHASE_DEAD)
|
|
|
|
bundle_Close(SignalBundle, NULL, 1);
|
1995-09-02 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-05-21 21:49:08 +00:00
|
|
|
AbortProgram(int excode)
|
1995-09-02 17:20:54 +00:00
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
server_Close(SignalBundle);
|
1997-11-09 06:22:49 +00:00
|
|
|
ID0unlink(pid_filename);
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
|
|
|
|
bundle_Close(SignalBundle, NULL, 1);
|
|
|
|
bundle_Destroy(SignalBundle);
|
|
|
|
log_Close();
|
1995-01-31 06:29:58 +00:00
|
|
|
exit(excode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
CloseConnection(int signo)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-06-23 23:10:13 +00:00
|
|
|
/* NOTE, these are manual, we've done a setsid() */
|
1998-05-21 21:49:08 +00:00
|
|
|
sig_signal(SIGINT, SIG_IGN);
|
|
|
|
log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
|
|
|
|
bundle_Down(SignalBundle);
|
|
|
|
sig_signal(SIGINT, CloseConnection);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
CloseSession(int signo)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogPHASE, "Signal %d, terminate.\n", signo);
|
1997-08-25 00:29:32 +00:00
|
|
|
Cleanup(EX_TERM);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
static pid_t BGPid = 0;
|
|
|
|
|
1995-09-02 17:20:54 +00:00
|
|
|
static void
|
1998-05-21 21:49:08 +00:00
|
|
|
KillChild(int signo)
|
1995-09-02 17:20:54 +00:00
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogPHASE, "Parent: Signal %d\n", signo);
|
|
|
|
kill(BGPid, SIGINT);
|
1995-09-02 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-05-21 21:49:08 +00:00
|
|
|
TerminalCont(int signo)
|
1995-09-02 17:20:54 +00:00
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
signal(SIGCONT, SIG_DFL);
|
|
|
|
prompt_Continue(SignalPrompt);
|
1995-09-02 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
static void
|
1998-05-21 21:49:08 +00:00
|
|
|
TerminalStop(int signo)
|
1997-06-25 19:30:05 +00:00
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
prompt_Suspend(SignalPrompt);
|
|
|
|
signal(SIGCONT, TerminalCont);
|
|
|
|
raise(SIGSTOP);
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
|
|
|
|
1997-11-09 22:07:29 +00:00
|
|
|
static void
|
|
|
|
BringDownServer(int signo)
|
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
/* Drops all child prompts too ! */
|
|
|
|
server_Close(SignalBundle);
|
1997-11-09 22:07:29 +00:00
|
|
|
}
|
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
static const char *
|
1997-05-19 02:00:16 +00:00
|
|
|
ex_desc(int ex)
|
|
|
|
{
|
|
|
|
static char num[12];
|
1997-11-22 03:37:54 +00:00
|
|
|
static const char *desc[] = {
|
|
|
|
"normal", "start", "sock", "modem", "dial", "dead", "done",
|
|
|
|
"reboot", "errdead", "hangup", "term", "nodial", "nologin"
|
|
|
|
};
|
1997-05-19 02:00:16 +00:00
|
|
|
|
1997-12-24 09:29:17 +00:00
|
|
|
if (ex >= 0 && ex < sizeof desc / sizeof *desc)
|
1997-05-19 02:00:16 +00:00
|
|
|
return desc[ex];
|
|
|
|
snprintf(num, sizeof num, "%d", ex);
|
|
|
|
return num;
|
|
|
|
}
|
1995-09-02 17:20:54 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
Usage(void)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1996-12-03 21:38:52 +00:00
|
|
|
fprintf(stderr,
|
1997-11-22 03:37:54 +00:00
|
|
|
"Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]"
|
|
|
|
#ifndef NOALIAS
|
|
|
|
" [ -alias ]"
|
|
|
|
#endif
|
|
|
|
" [system]\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
1997-11-17 01:13:41 +00:00
|
|
|
static char *
|
1998-05-21 21:49:08 +00:00
|
|
|
ProcessArgs(int argc, char **argv, int *mode)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
int optc, labelrequired;
|
1995-01-31 06:29:58 +00:00
|
|
|
char *cp;
|
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
optc = labelrequired = 0;
|
|
|
|
*mode = PHYS_MANUAL;
|
1995-01-31 06:29:58 +00:00
|
|
|
while (argc > 0 && **argv == '-') {
|
|
|
|
cp = *argv + 1;
|
1997-11-11 22:58:14 +00:00
|
|
|
if (strcmp(cp, "auto") == 0) {
|
1998-05-21 21:49:08 +00:00
|
|
|
*mode = PHYS_DEMAND;
|
|
|
|
labelrequired = 1;
|
1997-11-11 22:58:14 +00:00
|
|
|
} else if (strcmp(cp, "background") == 0) {
|
1998-05-21 21:49:08 +00:00
|
|
|
*mode = PHYS_1OFF;
|
|
|
|
labelrequired = 1;
|
|
|
|
} else if (strcmp(cp, "direct") == 0)
|
|
|
|
*mode = PHYS_DIRECT;
|
|
|
|
else if (strcmp(cp, "dedicated") == 0)
|
|
|
|
*mode = PHYS_DEDICATED;
|
|
|
|
else if (strcmp(cp, "ddial") == 0) {
|
|
|
|
*mode = PHYS_PERM;
|
|
|
|
labelrequired = 1;
|
1997-11-11 22:58:14 +00:00
|
|
|
} else if (strcmp(cp, "alias") == 0) {
|
1998-05-21 21:49:08 +00:00
|
|
|
#ifndef NOALIAS
|
|
|
|
if (alias_Load() != 0)
|
1997-11-22 03:37:54 +00:00
|
|
|
#endif
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogWARN, "Cannot load alias library\n");
|
|
|
|
optc--; /* this option isn't exclusive */
|
1997-08-25 00:29:32 +00:00
|
|
|
} else
|
1995-01-31 06:29:58 +00:00
|
|
|
Usage();
|
|
|
|
optc++;
|
1997-08-25 00:29:32 +00:00
|
|
|
argv++;
|
|
|
|
argc--;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
if (argc > 1) {
|
1998-05-21 21:49:08 +00:00
|
|
|
fprintf(stderr, "You may specify only one system label.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optc > 1) {
|
1998-05-21 21:49:08 +00:00
|
|
|
fprintf(stderr, "You may specify only one mode.\n");
|
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (labelrequired && argc != 1) {
|
|
|
|
fprintf(stderr, "Destination system must be specified in"
|
|
|
|
" auto, background or ddial mode.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
exit(EX_START);
|
|
|
|
}
|
1997-11-17 01:13:41 +00:00
|
|
|
|
|
|
|
return argc == 1 ? *argv : NULL; /* Don't SetLabel yet ! */
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
int
|
1997-08-25 00:29:32 +00:00
|
|
|
main(int argc, char **argv)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-05-11 10:23:16 +00:00
|
|
|
FILE *lockfile;
|
1997-11-17 01:13:41 +00:00
|
|
|
char *name, *label;
|
1998-05-21 21:49:08 +00:00
|
|
|
int nfds, mode;
|
|
|
|
struct bundle *bundle;
|
|
|
|
struct prompt *prompt;
|
1997-12-17 21:21:47 +00:00
|
|
|
|
|
|
|
nfds = getdtablesize();
|
|
|
|
if (nfds >= FD_SETSIZE)
|
|
|
|
/*
|
|
|
|
* If we've got loads of file descriptors, make sure they're all
|
|
|
|
* closed. If they aren't, we may end up with a seg fault when our
|
|
|
|
* `fd_set's get too big when select()ing !
|
|
|
|
*/
|
|
|
|
while (--nfds > 2)
|
|
|
|
close(nfds);
|
1997-06-09 03:27:43 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
name = strrchr(argv[0], '/');
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Open(name ? name + 1 : argv[0]);
|
1997-12-30 23:22:31 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1998-05-21 21:49:08 +00:00
|
|
|
label = ProcessArgs(argc, argv, &mode);
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
/*
|
|
|
|
* A FreeBSD hack to dodge a bug in the tty driver that drops output
|
|
|
|
* occasionally.... I must find the real reason some time. To display
|
|
|
|
* the dodgy behaviour, comment out this bit, make yourself a large
|
|
|
|
* routing table and then run ppp in interactive mode. The `show route'
|
|
|
|
* command will drop chunks of data !!!
|
|
|
|
*/
|
|
|
|
if (mode == PHYS_MANUAL) {
|
|
|
|
close(STDIN_FILENO);
|
|
|
|
if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
|
|
|
|
fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Allow output for the moment (except in direct mode) */
|
|
|
|
if (mode == PHYS_DIRECT)
|
|
|
|
prompt = NULL;
|
|
|
|
else {
|
|
|
|
const char *m;
|
|
|
|
|
|
|
|
SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
|
|
|
|
if (mode == PHYS_PERM)
|
|
|
|
m = "direct dial";
|
|
|
|
else if (mode & PHYS_1OFF)
|
|
|
|
m = "background";
|
|
|
|
else if (mode & PHYS_DEMAND)
|
|
|
|
m = "auto";
|
|
|
|
else if (mode & PHYS_DEDICATED)
|
|
|
|
m = "dedicated";
|
|
|
|
else if (mode & PHYS_MANUAL)
|
|
|
|
m = "interactive";
|
|
|
|
else
|
|
|
|
m = NULL;
|
|
|
|
|
|
|
|
if (m)
|
|
|
|
prompt_Printf(prompt, "Working in %s mode\n", m);
|
|
|
|
}
|
1997-11-11 22:58:14 +00:00
|
|
|
|
1997-11-09 06:22:49 +00:00
|
|
|
ID0init();
|
1997-11-13 15:35:06 +00:00
|
|
|
if (ID0realuid() != 0) {
|
|
|
|
char conf[200], *ptr;
|
|
|
|
|
|
|
|
snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE);
|
|
|
|
do {
|
|
|
|
if (!access(conf, W_OK)) {
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n", conf);
|
1997-11-13 15:35:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr = conf + strlen(conf)-2;
|
|
|
|
while (ptr > conf && *ptr != '/')
|
|
|
|
*ptr-- = '\0';
|
|
|
|
} while (ptr >= conf);
|
|
|
|
}
|
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
if (!system_IsValid(label, prompt, mode)) {
|
1997-11-11 22:58:14 +00:00
|
|
|
fprintf(stderr, "You may not use ppp in this mode with this label\n");
|
1998-05-21 21:49:08 +00:00
|
|
|
if (mode == PHYS_DIRECT) {
|
1997-11-13 14:44:06 +00:00
|
|
|
const char *l;
|
1997-11-17 01:13:41 +00:00
|
|
|
l = label ? label : "default";
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogWARN, "Label %s rejected -direct connection\n", l);
|
1997-11-13 14:44:06 +00:00
|
|
|
}
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Close();
|
1997-11-11 22:58:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-05-23 22:24:50 +00:00
|
|
|
if ((bundle = bundle_Create(TUN_PREFIX, mode)) == NULL) {
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno));
|
1997-06-25 19:30:05 +00:00
|
|
|
return EX_START;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-05-23 22:24:50 +00:00
|
|
|
if (prompt) {
|
|
|
|
prompt->bundle = bundle; /* couldn't do it earlier */
|
|
|
|
prompt_Printf(prompt, "Using interface: %s\n", bundle->ifp.Name);
|
|
|
|
}
|
1998-05-21 21:49:08 +00:00
|
|
|
SignalBundle = bundle;
|
1997-11-11 22:58:14 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
if (system_Select(bundle, "default", CONFFILE, prompt) < 0)
|
|
|
|
prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
|
|
|
|
|
|
|
|
sig_signal(SIGHUP, CloseSession);
|
|
|
|
sig_signal(SIGTERM, CloseSession);
|
|
|
|
sig_signal(SIGINT, CloseConnection);
|
|
|
|
sig_signal(SIGQUIT, CloseSession);
|
|
|
|
sig_signal(SIGALRM, SIG_IGN);
|
1997-04-09 17:35:54 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
1998-05-21 21:49:08 +00:00
|
|
|
|
|
|
|
if (mode == PHYS_MANUAL)
|
|
|
|
sig_signal(SIGTSTP, TerminalStop);
|
|
|
|
|
|
|
|
sig_signal(SIGUSR2, BringDownServer);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-11-17 01:13:41 +00:00
|
|
|
if (label) {
|
|
|
|
/*
|
1998-05-21 21:49:08 +00:00
|
|
|
* Set label both before and after system_Select !
|
|
|
|
* This way, "set enddisc label" works during system_Select, and we
|
|
|
|
* also end up with the correct label if we have embedded load
|
|
|
|
* commands.
|
1997-11-17 01:13:41 +00:00
|
|
|
*/
|
1998-05-21 21:49:08 +00:00
|
|
|
bundle_SetLabel(bundle, label);
|
|
|
|
if (system_Select(bundle, label, CONFFILE, prompt) < 0) {
|
|
|
|
prompt_Printf(prompt, "Destination system (%s) not found.\n", label);
|
|
|
|
AbortProgram(EX_START);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-05-21 21:49:08 +00:00
|
|
|
bundle_SetLabel(bundle, label);
|
|
|
|
if (mode == PHYS_DEMAND &&
|
|
|
|
bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) {
|
|
|
|
prompt_Printf(prompt, "You must \"set ifaddr\" with a peer address "
|
|
|
|
"in label %s for auto mode.\n", label);
|
|
|
|
AbortProgram(EX_START);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-05-21 21:49:08 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
if (mode != PHYS_MANUAL) {
|
|
|
|
if (mode != PHYS_DIRECT) {
|
|
|
|
int bgpipe[2];
|
1996-12-22 17:29:33 +00:00
|
|
|
pid_t bgpid;
|
1995-10-08 14:57:32 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
if (mode == PHYS_1OFF && pipe(bgpipe)) {
|
|
|
|
log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
|
|
|
|
AbortProgram(EX_SOCK);
|
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
bgpid = fork();
|
1996-12-22 17:29:33 +00:00
|
|
|
if (bgpid == -1) {
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogERROR, "fork: %s\n", strerror(errno));
|
|
|
|
AbortProgram(EX_SOCK);
|
1996-12-22 17:29:33 +00:00
|
|
|
}
|
1998-05-21 21:49:08 +00:00
|
|
|
|
1996-12-22 17:29:33 +00:00
|
|
|
if (bgpid) {
|
|
|
|
char c = EX_NORMAL;
|
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
if (mode == PHYS_1OFF) {
|
|
|
|
close(bgpipe[1]);
|
1996-12-22 17:29:33 +00:00
|
|
|
BGPid = bgpid;
|
1998-05-21 21:49:08 +00:00
|
|
|
/* If we get a signal, kill the child */
|
|
|
|
signal(SIGHUP, KillChild);
|
|
|
|
signal(SIGTERM, KillChild);
|
|
|
|
signal(SIGINT, KillChild);
|
|
|
|
signal(SIGQUIT, KillChild);
|
|
|
|
|
|
|
|
/* Wait for our child to close its pipe before we exit */
|
|
|
|
if (read(bgpipe[0], &c, 1) != 1) {
|
|
|
|
prompt_Printf(prompt, "Child exit, no status.\n");
|
|
|
|
log_Printf(LogPHASE, "Parent: Child exit, no status.\n");
|
1997-05-19 02:00:16 +00:00
|
|
|
} else if (c == EX_NORMAL) {
|
1998-05-21 21:49:08 +00:00
|
|
|
prompt_Printf(prompt, "PPP enabled.\n");
|
|
|
|
log_Printf(LogPHASE, "Parent: PPP enabled.\n");
|
1997-05-19 02:00:16 +00:00
|
|
|
} else {
|
1998-05-21 21:49:08 +00:00
|
|
|
prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c));
|
|
|
|
log_Printf(LogPHASE, "Parent: Child failed (%s).\n",
|
1997-08-25 00:29:32 +00:00
|
|
|
ex_desc((int) c));
|
|
|
|
}
|
1998-05-21 21:49:08 +00:00
|
|
|
close(bgpipe[0]);
|
1996-12-22 17:29:33 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return c;
|
1998-05-21 21:49:08 +00:00
|
|
|
} else if (mode == PHYS_1OFF) {
|
|
|
|
close(bgpipe[0]);
|
|
|
|
bundle->notify.fd = bgpipe[1];
|
|
|
|
}
|
1997-06-11 03:57:51 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
/* -auto, -dedicated, -ddial & -background */
|
|
|
|
prompt_Destroy(prompt, 0);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
1997-12-30 23:22:31 +00:00
|
|
|
close(STDIN_FILENO);
|
1998-05-21 21:49:08 +00:00
|
|
|
setsid();
|
|
|
|
} else {
|
|
|
|
/* -direct: STDIN_FILENO gets used by modem_Open */
|
|
|
|
prompt_TtyInit(NULL);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
1997-06-16 13:24:59 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
} else {
|
1998-05-21 21:49:08 +00:00
|
|
|
/* Interactive mode */
|
1997-12-30 23:22:31 +00:00
|
|
|
close(STDERR_FILENO);
|
1998-05-21 21:49:08 +00:00
|
|
|
prompt_TtyInit(prompt);
|
|
|
|
prompt_TtyCommandMode(prompt);
|
|
|
|
prompt_Required(prompt);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-09-22 00:46:56 +00:00
|
|
|
|
1997-12-24 09:29:17 +00:00
|
|
|
snprintf(pid_filename, sizeof pid_filename, "%stun%d.pid",
|
1998-05-21 21:49:08 +00:00
|
|
|
_PATH_VARRUN, bundle->unit);
|
1997-11-09 06:22:49 +00:00
|
|
|
lockfile = ID0fopen(pid_filename, "w");
|
|
|
|
if (lockfile != NULL) {
|
1997-09-22 00:46:56 +00:00
|
|
|
fprintf(lockfile, "%d\n", (int) getpid());
|
|
|
|
fclose(lockfile);
|
1998-01-29 00:44:15 +00:00
|
|
|
}
|
|
|
|
#ifndef RELEASE_CRUNCH
|
|
|
|
else
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogALERT, "Warning: Can't create %s: %s\n",
|
1997-09-22 00:46:56 +00:00
|
|
|
pid_filename, strerror(errno));
|
1998-01-29 00:44:15 +00:00
|
|
|
#endif
|
1997-09-22 00:46:56 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode));
|
|
|
|
DoLoop(bundle, prompt);
|
|
|
|
AbortProgram(EX_NORMAL);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
return EX_NORMAL;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-05-21 21:49:08 +00:00
|
|
|
DoLoop(struct bundle *bundle, struct prompt *prompt)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
fd_set rfds, wfds, efds;
|
1998-05-21 21:49:08 +00:00
|
|
|
int i, nfds;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
do {
|
1997-03-10 06:21:02 +00:00
|
|
|
nfds = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_ZERO(&efds);
|
1995-03-11 15:18:55 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
sig_Handle();
|
1995-09-18 12:41:52 +00:00
|
|
|
|
1998-05-23 22:24:50 +00:00
|
|
|
/* All our datalinks, the tun device and the MP socket */
|
1998-05-21 21:49:08 +00:00
|
|
|
descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds);
|
1998-05-23 22:24:50 +00:00
|
|
|
|
|
|
|
/* All our prompts and the diagnostic socket */
|
|
|
|
descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds);
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
if (bundle_IsDead(bundle))
|
|
|
|
/* Don't select - we'll be here forever */
|
|
|
|
break;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
i = select(nfds, &rfds, &wfds, &efds, NULL);
|
1997-01-29 01:27:58 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
if (i == 0)
|
1997-08-25 00:29:32 +00:00
|
|
|
continue;
|
1998-05-21 21:49:08 +00:00
|
|
|
else if (i < 0) {
|
|
|
|
if (errno == EINTR)
|
1997-04-09 17:35:54 +00:00
|
|
|
continue;
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
|
|
|
|
if (log_IsKept(LogTIMER)) {
|
|
|
|
struct timeval t;
|
|
|
|
|
|
|
|
for (i = 0; i <= nfds; i++) {
|
|
|
|
if (FD_ISSET(i, &rfds)) {
|
|
|
|
log_Printf(LogTIMER, "Read set contains %d\n", i);
|
|
|
|
FD_CLR(i, &rfds);
|
|
|
|
t.tv_sec = t.tv_usec = 0;
|
|
|
|
if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
|
|
|
|
log_Printf(LogTIMER, "The culprit !\n");
|
|
|
|
break;
|
|
|
|
}
|
1997-12-17 21:21:56 +00:00
|
|
|
}
|
1998-05-21 21:49:08 +00:00
|
|
|
if (FD_ISSET(i, &wfds)) {
|
|
|
|
log_Printf(LogTIMER, "Write set contains %d\n", i);
|
|
|
|
FD_CLR(i, &wfds);
|
|
|
|
t.tv_sec = t.tv_usec = 0;
|
|
|
|
if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
|
|
|
|
log_Printf(LogTIMER, "The culprit !\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (FD_ISSET(i, &efds)) {
|
|
|
|
log_Printf(LogTIMER, "Error set contains %d\n", i);
|
|
|
|
FD_CLR(i, &efds);
|
|
|
|
t.tv_sec = t.tv_usec = 0;
|
|
|
|
if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
|
|
|
|
log_Printf(LogTIMER, "The culprit !\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-05-21 21:49:08 +00:00
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
for (i = 0; i <= nfds; i++)
|
|
|
|
if (FD_ISSET(i, &efds)) {
|
|
|
|
log_Printf(LogALERT, "Exception detected on descriptor %d\n", i);
|
|
|
|
break;
|
1997-08-21 16:21:39 +00:00
|
|
|
}
|
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
if (i <= nfds)
|
|
|
|
break;
|
1997-12-28 02:46:26 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
if (descriptor_IsSet(&server.desc, &rfds))
|
|
|
|
descriptor_Read(&server.desc, bundle, &rfds);
|
|
|
|
|
|
|
|
if (descriptor_IsSet(&bundle->desc, &wfds))
|
|
|
|
descriptor_Write(&bundle->desc, bundle, &wfds);
|
|
|
|
|
|
|
|
if (descriptor_IsSet(&bundle->desc, &rfds))
|
|
|
|
descriptor_Read(&bundle->desc, bundle, &rfds);
|
|
|
|
} while (bundle_CleanDatalinks(bundle), !bundle_IsDead(bundle));
|
|
|
|
|
|
|
|
log_Printf(LogDEBUG, "DoLoop done.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|