/* Allocate a uniq XID for a name (a vserver). A database file is maintained in /var/run/vctxalloc. The dynamic XID allocation of the kernel is not used anymore. vallocctx set name */ #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; static const char *statefile = "/var/run/vctxalloc"; static const char *lockfile = "/var/run/vctxalloc.lock"; static void usage() { fprintf (stderr,"vallocctx version %s\n",VERSION); fprintf (stderr, "\n" "vallocctx get name\n" ); } static int vallocctx_find (vector &tb, const char *name) { int ret = -1; for (vector::iterator it = tb.begin(); it != tb.end(); it++){ if (strcmp(it->c_str(),name)==0){ ret = it - tb.begin(); break; } } return ret; } static void vallocctx_read (vector &tb) { FILE *fin = fopen (statefile,"r"); if (fin != NULL){ char line[1000]; while (fgets(line,sizeof(line)-1,fin)!=NULL){ char *pt = strchr(line,'\n'); if (pt != NULL) *pt = '\0'; if (line[0] > ' '){ tb.push_back(line); } } fclose (fin); } } static void vallocctx_write (vector &tb) { FILE *fout = fopen (statefile,"w"); if (fout != NULL){ for (vector::iterator it = tb.begin(); it != tb.end(); it++){ fprintf (fout,"%s\n",it->c_str()); } fclose (fout); } } #include "vipalias.lock.h" int main (int argc, char *argv[]) { int ret = -1; vector tb; if (argc == 3 && strcmp(argv[1],"get")==0){ const char *name = argv[2]; if (vipalias_lock() != -1){ vallocctx_read(tb); int pos = vallocctx_find (tb,name); if (pos == -1){ pos = vallocctx_find(tb,"-"); if (pos != -1){ tb[pos] = name; }else{ pos = tb.size(); tb.push_back(name); } vallocctx_write (tb); } printf ("%d\n",pos); vipalias_unlock(); ret = 0; } }else if (argc == 3 && strcmp(argv[1],"unset")==0){ const char *name = argv[2]; if (vipalias_lock() != -1){ vallocctx_read(tb); int pos = vallocctx_find (tb,name); if (pos != -1){ tb[pos] = "-"; vallocctx_write (tb); ret = 0; } vipalias_unlock(); } }else if (argc == 2 && strcmp(argv[1],"list")==0){ if (vipalias_lock() != -1){ vallocctx_read(tb); int n = 0; for (vector::iterator it=tb.begin(); it != tb.end(); it++,n++){ if (*it != "-"){ printf ("%d %s\n",n,it->c_str()); } } vipalias_unlock(); ret = 0; } }else{ usage(); } return ret; }