--- net-scripts-0.4.9.1/src/ppp-watch.c 2001-03-05 02:22:06 +0200 +++ net-scripts-0.4.9.1/src/ppp-watch.c 2005-10-21 10:31:23 +0300 @@ -75,6 +75,7 @@ #include #include #include +#include #include "shvar.h" #include "alloc.h" @@ -116,6 +117,43 @@ } +static int +get_pppd_pid(char *device) { + int i; + glob_t globbuf; + int ge = glob("/proc/*/cmdline", 0, NULL, &globbuf); + if (!ge) { + for (i = 0; i < globbuf.gl_pathc; ++i) { + FILE *fcmdline; + char cmdline[1024]; // hope this will be enough + if (fcmdline = fopen(globbuf.gl_pathv[i], "r")) { + int num_read; + num_read = fread(cmdline, 1, 1024, fcmdline); + if (num_read > 0 && !strcmp(cmdline, "/usr/sbin/pppd")) { + int prev_linkname = 0; + char *p = cmdline; + while (*p && p - cmdline < 1024) { + if (prev_linkname && !strcmp(p, device)) { + int retval; + globbuf.gl_pathv[i][strlen(globbuf.gl_pathv[i])-8] = 0; + retval = atoi(globbuf.gl_pathv[i]+6); + globfree(&globbuf); + fclose(fcmdline); + return retval; + } + prev_linkname = !strcmp(p, "linkname"); + p += strlen(p) + 1; + } + } + fclose(fcmdline); + } + } + globfree(&globbuf); + return 0; + } + else + return -1; +} static void detach(int now, int parentExitCode, char *device) { @@ -550,6 +588,20 @@ if (physicalDevice) { free(physicalDevice); physicalDevice = NULL; } physicalDevice = pppLogicalToPhysical(&pppdPid, real_device); if (physicalDevice) { free(physicalDevice); physicalDevice = NULL; } + if (!pppdPid && get_pppd_pid(device)) + { + // We suspect that pppd is running, but haven't created + // .pid file yet. So we'll wait for 10 seconds, checking + // for .pid file every 10 milliseconds. + int i; + for (i = 0; i < 1000 && !pppdPid && get_pppd_pid(device) !=0 ; ++i) + { + usleep(10000); + physicalDevice = pppLogicalToPhysical(&pppdPid, real_device); + if (physicalDevice) { free(physicalDevice); physicalDevice = NULL; } + } + sleep(2); // Else pppd may hangup, seems there's a bug in it + } if (!pppdPid) cleanExit(35); kill(pppdPid, sendsig); if (sendsig == SIGKILL) {