📄 setpe.c
字号:
/* Author(s): Amnon Shiloh and Oren Laadan for Mosix *//* Code derived from previous work by Amnon Shiloh and Oren Laadan. *//* Adapted to OpenMosix from Mosix and bugfixing by David Santo Orcero *//* irbis@orcero.org http://www.orcero.org/irbis *//* Mosix is (c) of prof. Amnon Barak http://www.mosix.org *//* Original code is (c) of prof. Amnon Barak http://www.mosix.org *//* OpenMosix is (c) of Moshe Bar http://www.openmosix.com *//* Each respective trademark is of its own owner *//* All rights reserved. *//* This software is distributed under GPL 2 *//* THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTY IS ASSUMED. *//* NO LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING *//* FROM THE USE OF THIS SOFTWARE WILL BE ACCEPTED. IT CAN BURN *//* YOUR HARD DISK, ERASE ALL YOUR DATA AND BROKE DOWN YOUR *//* MICROWAVE OVEN. YOU ARE ADVISED. *//* THIS SOFTWARE IS PROVIDED IN ITS "AS IS" CONDITION, WITH NO WARRANTY *//* WHATSOEVER. NO LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING*//* FROM THE USE OF THIS SOFTWARE WILL BE ACCEPTED. */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <signal.h>#include <fcntl.h>#include <sys/types.h>#include <sys/param.h>#include <sys/time.h>#include <sys/errno.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <libmosix.h>#include <mos.h>#define ERR_OK 0#define ERR_PROG 1#define ERR_PROG_PID 3#define ERR_OTHER 102char *pname;int proc_mosix_admin_config_fd;int proc_mosix_admin_mospe_fd;struct mosixnet mosnet[MAX_MOSNET_ENTS + 1];char hostnamebuf[MAXHOSTNAMELEN + 1];char check;char two_phase = -1;voidnot_mosix (){ fprintf (stderr, "Error: this is not OpenMosix!\n"); exit (ERR_OTHER);}void usage (void);int table_read (int *pe, int *ready_pe, int *nents, struct mosixnet *mosnet);void setpe_off (void);void setpe_read (FILE *);void setpe_write (FILE *, int, int);void set_gateways (int);int write_file (int, struct mosixnet *, int);void ip_display (int, FILE *);void ip_setpe (int, FILE *);void ip_check_table (int, int);int ip_need_two_phase (int, int, int, struct mosixnet *, int);#define GATEWAYS_FILENAME "/proc/hpc/admin/gateways"#define CONFIG_FILENAME "/proc/hpc/admin/config"#define MOSPE_FILENAME "/proc/hpc/admin/mospe"intmain (int c, char *v[]){ int pe = 0; int opt; int net_type = AF_INET; const char *filename = "-"; FILE *table; int write = -1; int gateways = -1; int off = 0; pname = v[0]; for (opt = strlen (pname); opt--;) if (pname[opt] == '/') { pname += opt + 1; break; } while ((opt = getopt (c, v, "f:g:n:o:p:crwW")) != EOF) { if (off) usage (); switch (opt) { case 'o': if (!optarg || strcmp (optarg, "ff")) usage (); off = 1; write = 1; break; case 'n': if (!optarg || write == 0) usage (); write = 1; if (!strcmp (optarg, "inet")) break; usage (); case 'p': if (!optarg || write == 0) usage (); write = 1; if (!(pe = atoi (optarg))) { fprintf (stderr, "%s: Invalid node " "number\n", pname); } break; case 'f': if (!optarg) usage (); filename = optarg; break; case 'c': if (write == 0) usage (); write = 1; check = 1; break; case 'g': if (!optarg || gateways != -1 || sscanf (optarg, "%d%c", &gateways, (char *) &gateways) != 1 || gateways < 0 || gateways > 2) usage (); break; case 'w': if (write == 0 || two_phase == 1) usage (); write = 1; two_phase = 0; break; case 'W': if (write == 0 || two_phase == 0) usage (); write = 1; two_phase = 1; break; case 'r': if (write == 1) usage (); write = 0; break; default: usage (); } } if (optind < c) usage (); if (write == -1) usage (); if (!strcmp (filename, "-")) table = (write ? stdin : stdout); else if (!(table = fopen (filename, write ? "r" : "w"))) { perror (filename); exit (ERR_OTHER); } proc_mosix_admin_mospe_fd = open (MOSPE_FILENAME, write ? O_RDWR : O_RDONLY); proc_mosix_admin_config_fd = open (CONFIG_FILENAME, write ? O_RDWR : O_RDONLY); if (proc_mosix_admin_mospe_fd < 0 || proc_mosix_admin_config_fd < 0) not_mosix (); if (off) setpe_off (); else if (write) setpe_write (table, pe, net_type); else setpe_read (table); if (gateways != -1) set_gateways (gateways); exit (ERR_OK);}voidsetpe_off (void){ if (write (proc_mosix_admin_mospe_fd, "0", 1) == 1) return; perror ("unconfiguring OpenMosix"); exit (ERR_OTHER);}inttable_read (int *pe, int *pe_ready, int *nents, struct mosixnet *mosnet){ int i, j, error; char str[20]; error = read (proc_mosix_admin_mospe_fd, str, 20); if (error < 0) { if (errno == ENOSYS) return (errno); perror ("read"); exit (ERR_OTHER); } *pe = atoi (str); if (!(*pe)) { *nents = *pe = 0; return (0); } for (i = 0, j = 0; i < error; i++) if (str[i] == '(') { j = i + 1; str[i] = '\0'; } else if (str[i] == ')') str[i] = '\0'; if (j) *pe_ready = atoi (str + j); else *pe_ready = 0; error = read (proc_mosix_admin_config_fd, (char *) mosnet, sizeof (struct mosixnet) * MAX_MOSNET_ENTS); if (error < 0) { perror ("read"); exit (ERR_OTHER); } *nents = error / sizeof (struct mosixnet); return (0);}voidsetpe_read (FILE * table){ int pe, pe_ready = -1; int error; int net, nents; error = table_read (&pe, &pe_ready, &nents, mosnet); if (error) { perror (""); exit (ERR_OTHER); } if (!pe) { fprintf (table, "OpenMosix is currently disabled\n"); exit (ERR_PROG_PID); } fprintf (table, "This is OpenMosix node #%d", pe); if (pe_ready > 0) fprintf (table, " (PE already set to %d)", pe_ready); fprintf (table, "\n"); net = mosnet[0].saddr.sa_family; switch (net) { case AF_INET: ip_display (nents, table); break; default: fprintf (stderr, "%s: severe problem ! unknown network " "type %d\n", pname, net); exit (ERR_OTHER); }}voidsetpe_write (FILE * table, int pe, int net_type){ switch (net_type) { case AF_INET: ip_setpe (pe, table); break; default: fprintf (stderr, "%s: unknown network type %d\n", pname, net_type); exit (ERR_OTHER); }}intwrite_file (int pe, struct mosixnet *table, int nent){ int error; char str[10]; sprintf (str, "%d", pe); error = write (proc_mosix_admin_mospe_fd, str, strlen (str) + 1); if (error < 0) { perror ("The OpenMosix kernel refused to accept the node-number"); exit (ERR_PROG); } nent *= sizeof (struct mosixnet); error = write (proc_mosix_admin_config_fd, (char *) table, (size_t) nent); if (error < 0) { perror ("The OpenMosix kernel refused to accept the table"); return (-1); } return (0);}#define NET_TO_IP(t) (((struct sockaddr_in *) &(t).saddr)->sin_addr.s_addr)voidip_display (int nents, FILE * table){ struct hostent *hostent; u_long ipaddr; int i, n = 0, a = 0; char host[512]; fprintf (table, "Network protocol: %d (AF_INET)\n", AF_INET); for (i = 0; i < nents; i++) { ipaddr = NET_TO_IP (mosnet[i]); if (mosnet[i].cnt && (hostent = gethostbyaddr ((char *) &ipaddr, 4, AF_INET))) strcpy (host, hostent->h_name); else { unsigned int a = (ipaddr) & 0xff; unsigned int b = (ipaddr >> 8) & 0xff; unsigned int c = (ipaddr >> 16) & 0xff; unsigned int d = (ipaddr >> 24); sprintf (host, "%d.%d.%d.%d", a, b, c, d); } if (mosnet[i].cnt) { fprintf (table, "OpenMosix range %5d-%-5d begins at %s\n", mosnet[i].base, mosnet[i].base + mosnet[i].cnt - 1, host); n += mosnet[i].cnt; } else { fprintf (table, "Node %d has an alias: %s\n", mosnet[i].base, host); a++; } } fprintf (table, "Total configured: %d", n); if (a) fprintf (table, "; %d alias%s", a, a > 1 ? "es" : ""); fprintf (table, "\n"); exit (ERR_OK);}voidip_setpe (int pe, FILE * table){ char hostname[MAXHOSTNAMELEN + 1]; struct hostent *hostent; u_long myaddr, ipaddr; int err, n = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -