📄 setpe.c
字号:
char buf[513], host[512]; unsigned int a, b, c, d; struct mosixnet oldmosnet[MAX_MOSNET_ENTS]; int oldpe, oldready_pe, oldnents; char word[10]; while (fgets (buf, 512, table)) if (buf[0] && buf[0] != '#' && buf[0] != '\n') { buf[512] = '\0'; if (sscanf (buf, "%d %d.%d.%d.%d %d", &mosnet[n].base, &a, &b, &c, &d, &mosnet[n].cnt) == 6) { ip_line: NET_TO_IP (mosnet[n++]) = (a << 24) | (b << 16) | (c << 8) | d; check_cnt: if (mosnet[n].cnt < 0) { fprintf (stderr, "%s: Negative count\n", pname); exit (ERR_OTHER); } if (n > MAX_MOSNET_ENTS) { fprintf (stderr, "%s: Table Overflow\n", pname); exit (ERR_OTHER); } } else if (sscanf (buf, "%d %s %d", &mosnet[n].base, host, &mosnet[n].cnt) == 3) { host_line: if ((hostent = gethostbyname (host)) == NULL) { if ((ipaddr = inet_addr (host)) == -1) { herror (host); exit (ERR_OTHER); } } else { bcopy (hostent->h_addr, &ipaddr, sizeof (ipaddr)); ipaddr = ntohl (ipaddr); } NET_TO_IP (mosnet[n++]) = ipaddr; goto check_cnt; } else if (sscanf (buf, "%d %d.%d.%d.%d %10s", &mosnet[n].base, &a, &b, &c, &d, word) == 6 && !strcasecmp (word, "alias")) { mosnet[n].cnt = 0; goto ip_line; } else if (sscanf (buf, "%d %s %10s", &mosnet[n].base, host, word) == 3 && !strcasecmp (word, "alias")) { mosnet[n].cnt = 0; goto host_line; } else { if (buf[strlen (buf) - 1] == '\n') buf[strlen (buf) - 1] = '\0'; fprintf (stderr, "Bad Table Input: <%s>\n", buf); exit (ERR_OTHER); } } if (!n) { fprintf (stderr, "%s: the table is empty.\n", pname); exit (ERR_OTHER); } if (!pe) { hostname[MAXHOSTNAMELEN] = '\0'; if (gethostname (hostname, MAXHOSTNAMELEN) < 0) { perror ("setpe: gethostname"); exit (ERR_OTHER); } if ((hostent = gethostbyname (hostname)) == NULL) { if ((myaddr = inet_addr (hostname)) == -1) { herror (hostname); exit (ERR_OTHER); } } else { bcopy (hostent->h_addr, &myaddr, sizeof (myaddr)); myaddr = ntohl (myaddr); } for (a = 0; a < n; a++) if (myaddr >= NET_TO_IP (mosnet[a]) && myaddr < NET_TO_IP (mosnet[a]) + mosnet[a].cnt) break; if (a == n) { fprintf (stderr, "%s: the supplied table is well-" "formatted,\n", pname); fprintf (stderr, "but my IP address (%d.%d.%d.%d) " "is not there!\n", (unsigned int) (myaddr >> 24), (unsigned int) ((myaddr >> 16) & 0xff), (unsigned int) ((myaddr >> 8) & 0xff), (unsigned int) (myaddr & 0xff)); exit (ERR_OTHER); } pe = mosnet[a].base + myaddr - NET_TO_IP (mosnet[a]); } ip_check_table (n, pe); /* set network family - should be in the first entry */ mosnet[0].saddr.sa_family = AF_INET; /* lastly, the kernel expects network order for the IP's */ for (a = 0; a < n; a++) NET_TO_IP (mosnet[a]) = htonl (NET_TO_IP (mosnet[a])); if ((err = table_read (&oldpe, &oldready_pe, &oldnents, oldmosnet))) { if (err != ENOSYS) { perror (""); exit (ERR_OTHER); } /* else: silently ignore... we not configured anyway */ } if (!err) { if (ip_need_two_phase (pe, n, oldpe, oldmosnet, oldnents)) { if (!two_phase) { fprintf (stderr, "%s: The proposed change will modify the current node's mapping,\n", pname); fprintf (stderr, "requiring a full OpenMosix restart, including bringing all processes back home:\n"); fprintf (stderr, "If you are sure about the change, use 'setpe -W' (instead of 'setpe -w').\n"); exit (ERR_OTHER); }; }; } else { two_phase = 0; }; if (check) return; /* -- COMMIT POINT -- */ close (proc_mosix_admin_mospe_fd); close (proc_mosix_admin_config_fd); proc_mosix_admin_mospe_fd = open (MOSPE_FILENAME, O_WRONLY); proc_mosix_admin_config_fd = open (CONFIG_FILENAME, O_WRONLY); if (two_phase) { /* ok then, so - shutdown and try new configuration, and if failed - restore old one */ setpe_off (); close (proc_mosix_admin_mospe_fd); proc_mosix_admin_mospe_fd = open (MOSPE_FILENAME, O_WRONLY); } if (write_file (pe, mosnet, n) == -1 && two_phase) { /* FAILED */ close (proc_mosix_admin_mospe_fd); close (proc_mosix_admin_config_fd); proc_mosix_admin_mospe_fd = open (MOSPE_FILENAME, O_WRONLY); proc_mosix_admin_config_fd = open (CONFIG_FILENAME, O_WRONLY); (void) write_file (oldpe, oldmosnet, oldnents); fprintf (stderr, "%s: configuration restored!\n", pname); }}voidip_check_table (int n, int pe){ register int i, j; register struct mosixnet *t, *q; char foundme = 0; int aok; if (pe < 1) { fprintf (stderr, "%s: My node number < 1 ???\n", pname); exit (ERR_OTHER); } if (pe >= MOSIX_MAX) { fprintf (stderr, "%s: This node number is larger than" "the maximum (%d)\n", pname, MOSIX_MAX - 1); exit (ERR_OTHER); } if (n < 1) { fprintf (stderr, "%s: The table is Empty!\n", pname); exit (ERR_OTHER); } if (n > MAX_MOSNET_ENTS) { fprintf (stderr, "%s: Table size too big (max %d entries)\n", pname, MAX_MOSNET_ENTS); exit (ERR_OTHER); } for (t = mosnet, i = 0; i < n; i++, t++) { if (t->cnt < 0) { fprintf (stderr, "%s: Table contains a range with a negative " "number of nodes!\n", pname); exit (ERR_OTHER); } if (t->base < 1) { fprintf (stderr, "%s: Table contains OpenMosix 'node-#%d'!\n", pname, t->base); exit (ERR_OTHER); } if (t->base + t->cnt - 1 >= MOSIX_MAX) { fprintf (stderr, "%s: Table contains node-#%d (max=%d)!\n", pname, t->base + t->cnt - 1, MOSIX_MAX - 1); exit (ERR_OTHER); } if (NET_TO_IP (*t) + t->cnt < NET_TO_IP (*t)) { fprintf (stderr, "%s: Table contains an IP Address(es) Wrap!\n", pname); exit (ERR_OTHER); } if (t->cnt) { for (q = mosnet, j = 0; j < i; j++, q++) if (q->cnt) { if ((q->base < t->base && q->base + q->cnt > t->base) || (q->base >= t->base && q->base < t->base + t->cnt)) { fprintf (stderr, "%s: Table contains conflicting" " OpenMosix addresses!\n", pname); exit (ERR_OTHER); } if ((NET_TO_IP (*q) < NET_TO_IP (*t) && NET_TO_IP (*q) + q->cnt > NET_TO_IP (*t)) || (NET_TO_IP (*q) >= NET_TO_IP (*t) && NET_TO_IP (*q) < NET_TO_IP (*t) + t->cnt)) { fprintf (stderr, "%s: Table contains conflicting" " IP addresses!\n", pname); exit (ERR_OTHER); } } if (pe >= t->base && pe < t->base + t->cnt) foundme = 1; } else { aok = 0; for (q = mosnet, j = 0; j < n; j++, q++) if (j == i) continue; else if (q->cnt) { if (t->base >= q->base && t->base < q->base + q->cnt) aok = 1; if ((NET_TO_IP (*t) >= NET_TO_IP (*q) && NET_TO_IP (*t) < NET_TO_IP (*q) + q->cnt)) { fprintf (stderr, "%s: Table contains an IP " "conflict between a range and an alias!" "\n", pname); exit (ERR_OTHER); } } else if (NET_TO_IP (*t) == NET_TO_IP (*q)) { fprintf (stderr, "%s: Table contains a conflict: 2 or " "more aliases claim the same IP address!\n", pname); exit (ERR_OTHER); } if (!aok) { fprintf (stderr, "%s: Table contains an alias for node " "#%d - which is not included!\n", pname, t->base); exit (ERR_OTHER); } } } if (!foundme) { fprintf (stderr, "%s: the table is ok, but I'm not there!\n", pname); exit (ERR_OTHER); }}intip_need_two_phase (int pe, int nents, int oldpe, struct mosixnet *oldmosnet, int oldnents){ register int i; register struct mosixnet *t; u_long ip = 0, oldip = 0; /* * sufficient conditions for two phases: * (1) pe != oldpe (but: oldpe != 0, not startup, * pe != 0, not shutdown) * (2) ip != oldip */ if (!pe || !oldpe) return (0); if (pe != oldpe) return (1); /* find the ip's */ for (t = mosnet, i = 0; i < nents; i++, t++) { if (pe >= t->base && pe < t->base + t->cnt) ip = NET_TO_IP (*t) + pe - t->base; } for (t = oldmosnet, i = 0; i < oldnents; i++, t++) { if (oldpe >= t->base && oldpe < t->base + t->cnt) oldip = NET_TO_IP (*t) + oldpe - t->base; } if (!ip || !oldip) { fprintf (stderr, "%s: new IP or old IP is 0.0.0.0 ?!\n", pname); exit (ERR_OTHER); } if (ip != oldip) return (1); return (0);}voidset_gateways (int gateways){ char str[10]; int fd; if ((fd = open (GATEWAYS_FILENAME, O_RDWR)) < 0) { fprintf (stderr, "%s: Error - this version of OpenMosix does not" " support the '-g' option!\n", pname); exit (ERR_OTHER); } sprintf (str, "%d", gateways); if (write (fd, str, strlen (str) + 1) < 0) { perror ("writing the '-g' value"); exit (ERR_OTHER); } close (fd);}voidusage (){ fprintf (stderr, "OpenMosix's setpe usage: %s -[w|W] [-n inet] [-c] [-p pe] [-g {0|1|2}]" " [-f filename|-]\n" " or: %s -r [-f filename|-]\n" " or: %s -off\n", pname, pname, pname); exit (ERR_OK);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -