int SqliteClient::execute(const string& sql) { int result = sqlite3_exec(_sqliteDb, sql.c_str(), NULL, NULL, NULL); if(result != SQLITE_OK) { printErrorInfo("sqlite3_exec", sql); } return result; }
void SessionServer::run() { while(true) { switch(status) { case SessionServer::NLOGIN: rlogin(); break; case SessionServer::LOOPING: rloop(); break; case SessionServer::EXIT: printClosedInfo(); return; case SessionServer::ERROR: printErrorInfo(); return; } } }
int cgiMain() { char name[241] = {0}; char password[241] = {0}; char myname[128] = {0}; char mypswd[128] = {0}; char sql[256] = {0}; int rc = SQLITE_OK; char *szerrmsg = NULL; sqlite3 *db; struct result res; cgiHeaderContentType("text/html"); cgiFormString("password", password, 241); cgiFormString("name", name, 241); memset(res.name, 0, 128); memset(res.passwd, 0, 128); rc = sqlite3_open(dbname, &db); if (rc != SQLITE_OK) { fprintf(stderr, "Cannot open database: %s", dbname); sqlite3_close(db); } sprintf(sql, "select name, passwd from user"); rc = sqlite3_exec(db, sql, queryinfo, &res, &szerrmsg); if (rc != SQLITE_OK) { fprintf(stderr, "Execure '%s' failure!", sql); sqlite3_close(db); } else { strncpy(myname, res.name, 128); strncpy(mypswd, res.passwd, 128); } if (!strcmp(res.name, "") && !strcmp(res.passwd, "")) { strncpy(myname, "admin", 6); strncpy(mypswd, "63324455", 9); } if (!strcmp(name, myname) && !strcmp(password, mypswd)) { fprintf(cgiOut, "<html>\n"); fprintf(cgiOut, "<head><title>login success</title></head>\n"); fprintf(cgiOut, "<meta http-equiv=\"Refresh\" content=\"0;URL=http://192.168.1.253/main.htm\">\n"); } else { memset(sql, 0, 256); sprintf(sql, "User Name or Password Error!\n"); printErrorInfo(sql); sqlite3_close(db); return -1; } return 0; }
/** * Create a sample VM * * @param virtualBox VirtualBox instance object. */ void createVM(IVirtualBox *virtualBox) { nsresult rc; /* * First create a unnamed new VM. It will be unconfigured and not be saved * in the configuration until we explicitely choose to do so. */ nsCOMPtr<IMachine> machine; rc = virtualBox->CreateMachine(NULL, /* settings file */ NS_LITERAL_STRING("A brand new name").get(), 0, nsnull, /* groups (safearray)*/ nsnull, /* ostype */ nsnull, /* create flags */ getter_AddRefs(machine)); if (NS_FAILED(rc)) { printf("Error: could not create machine! rc=%08X\n", rc); return; } /* * Set some properties */ /* alternative to illustrate the use of string classes */ rc = machine->SetName(NS_ConvertUTF8toUTF16("A new name").get()); rc = machine->SetMemorySize(128); /* * Now a more advanced property -- the guest OS type. This is * an object by itself which has to be found first. Note that we * use the ID of the guest OS type here which is an internal * representation (you can find that by configuring the OS type of * a machine in the GUI and then looking at the <Guest ostype=""/> * setting in the XML file. It is also possible to get the OS type from * its description (win2k would be "Windows 2000") by getting the * guest OS type collection and enumerating it. */ nsCOMPtr<IGuestOSType> osType; rc = virtualBox->GetGuestOSType(NS_LITERAL_STRING("win2k").get(), getter_AddRefs(osType)); if (NS_FAILED(rc)) { printf("Error: could not find guest OS type! rc=%08X\n", rc); } else { machine->SetOSTypeId (NS_LITERAL_STRING("win2k").get()); } /* * Register the VM. Note that this call also saves the VM config * to disk. It is also possible to save the VM settings but not * register the VM. * * Also note that due to current VirtualBox limitations, the machine * must be registered *before* we can attach hard disks to it. */ rc = virtualBox->RegisterMachine(machine); if (NS_FAILED(rc)) { printf("Error: could not register machine! rc=%08X\n", rc); printErrorInfo(); return; } /* * In order to manipulate the registered machine, we must open a session * for that machine. Do it now. */ nsCOMPtr<ISession> session; { nsCOMPtr<nsIComponentManager> manager; rc = NS_GetComponentManager (getter_AddRefs (manager)); if (NS_FAILED(rc)) { printf("Error: could not get component manager! rc=%08X\n", rc); return; } rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID, nsnull, NS_GET_IID(ISession), getter_AddRefs(session)); if (NS_FAILED(rc)) { printf("Error, could not instantiate session object! rc=0x%x\n", rc); return; } rc = machine->LockMachine(session, LockType_Write); if (NS_FAILED(rc)) { printf("Error, could not lock the machine for the session! rc=0x%x\n", rc); return; } /* * After the machine is registered, the initial machine object becomes * immutable. In order to get a mutable machine object, we must query * it from the opened session object. */ rc = session->GetMachine(getter_AddRefs(machine)); if (NS_FAILED(rc)) { printf("Error, could not get machine session! rc=0x%x\n", rc); return; } } /* * Create a virtual harddisk */ nsCOMPtr<IMedium> hardDisk = 0; rc = virtualBox->CreateHardDisk(NS_LITERAL_STRING("VDI").get(), NS_LITERAL_STRING("TestHardDisk.vdi").get(), getter_AddRefs(hardDisk)); if (NS_FAILED(rc)) { printf("Failed creating a hard disk object! rc=%08X\n", rc); } else { /* * We have only created an object so far. No on disk representation exists * because none of its properties has been set so far. Let's continue creating * a dynamically expanding image. */ nsCOMPtr <IProgress> progress; com::SafeArray<MediumVariant_T> mediumVariant(sizeof(MediumVariant_T)*8); mediumVariant.push_back(MediumVariant_Standard); rc = hardDisk->CreateBaseStorage(100, // size in megabytes ComSafeArrayAsInParam(mediumVariant), getter_AddRefs(progress)); // optional progress object if (NS_FAILED(rc)) { printf("Failed creating hard disk image! rc=%08X\n", rc); } else { /* * Creating the image is done in the background because it can take quite * some time (at least fixed size images). We have to wait for its completion. * Here we wait forever (timeout -1) which is potentially dangerous. */ rc = progress->WaitForCompletion(-1); PRInt32 resultCode; progress->GetResultCode(&resultCode); if (NS_FAILED(rc) || NS_FAILED(resultCode)) { printf("Error: could not create hard disk! rc=%08X\n", NS_FAILED(rc) ? rc : resultCode); } else { /* * Now that it's created, we can assign it to the VM. */ rc = machine->AttachDevice(NS_LITERAL_STRING("IDE Controller").get(), // controller identifier 0, // channel number on the controller 0, // device number on the controller DeviceType_HardDisk, hardDisk); if (NS_FAILED(rc)) { printf("Error: could not attach hard disk! rc=%08X\n", rc); } } } } /* * It's got a hard disk but that one is new and thus not bootable. Make it * boot from an ISO file. This requires some processing. First the ISO file * has to be registered and then mounted to the VM's DVD drive and selected * as the boot device. */ nsCOMPtr<IMedium> dvdImage; rc = virtualBox->OpenMedium(NS_LITERAL_STRING("/home/vbox/isos/winnt4ger.iso").get(), DeviceType_DVD, AccessMode_ReadOnly, false /* fForceNewUuid */, getter_AddRefs(dvdImage)); if (NS_FAILED(rc)) printf("Error: could not open CD image! rc=%08X\n", rc); else { /* * Now assign it to our VM */ rc = machine->MountMedium(NS_LITERAL_STRING("IDE Controller").get(), // controller identifier 2, // channel number on the controller 0, // device number on the controller dvdImage, PR_FALSE); // aForce if (NS_FAILED(rc)) { printf("Error: could not mount ISO image! rc=%08X\n", rc); } else { /* * Last step: tell the VM to boot from the CD. */ rc = machine->SetBootOrder (1, DeviceType::DVD); if (NS_FAILED(rc)) { printf("Could not set boot device! rc=%08X\n", rc); } } } /* * Save all changes we've just made. */ rc = machine->SaveSettings(); if (NS_FAILED(rc)) { printf("Could not save machine settings! rc=%08X\n", rc); } /* * It is always important to close the open session when it becomes not * necessary any more. */ session->UnlockMachine(); }
static int check_load(printInfoStruct& printInfo) { if (l_Debug) std::wcout << L"Creating query and adding counter" << '\n'; PDH_HQUERY phQuery; PDH_STATUS err = PdhOpenQuery(NULL, NULL, &phQuery); if (!SUCCEEDED(err)) goto die; PDH_HCOUNTER phCounter; err = PdhAddEnglishCounter(phQuery, L"\\Processor(_Total)\\% Idle Time", NULL, &phCounter); if (!SUCCEEDED(err)) goto die; if (l_Debug) std::wcout << L"Collecting first batch of query data" << '\n'; err = PdhCollectQueryData(phQuery); if (!SUCCEEDED(err)) goto die; if (l_Debug) std::wcout << L"Sleep for one second" << '\n'; Sleep(1000); if (l_Debug) std::wcout << L"Collecting second batch of query data" << '\n'; err = PdhCollectQueryData(phQuery); if (!SUCCEEDED(err)) goto die; if (l_Debug) std::wcout << L"Creating formatted counter array" << '\n'; DWORD CounterType; PDH_FMT_COUNTERVALUE DisplayValue; err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue); if (SUCCEEDED(err)) { if (DisplayValue.CStatus == PDH_CSTATUS_VALID_DATA) { if (l_Debug) std::wcout << L"Recieved Value of " << DisplayValue.doubleValue << L" (idle)" << '\n'; printInfo.load = 100.0 - DisplayValue.doubleValue; } else { if (l_Debug) std::wcout << L"Received data was not valid\n"; goto die; } if (l_Debug) std::wcout << L"Finished collection. Cleaning up and returning" << '\n'; PdhCloseQuery(phQuery); return -1; } die: printErrorInfo(); if (phQuery) PdhCloseQuery(phQuery); return 3; }
int SqliteClient::executeSqlQuery(const string& sql, DataTable& table) { Locker locker(&_sqliteDbMutex); #if DEBUG Stopwatch sw("executeSqlQuery", 100); #endif sqlite3_stmt *stmt; int result = sqlite3_prepare_v2(_sqliteDb, sql.c_str(), sql.length(), &stmt, 0); if(result != SQLITE_OK) { printErrorInfo("sqlite3_prepare_v2", sql); return result; } //const char* name = sqlite3_column_table_name(stmt, 0); //table.setName(name != NULL ? name : "temp"); // todo: linke error for sqlite3_column_table_name. table.setName("temp"); #if DEBUG sw.setInfo(Convert::convertStr("executeSqlQuery, the table name is '%s'", table.getName().c_str())); #endif int columnCount = sqlite3_column_count(stmt); for (int i = 0; i < columnCount; i++) { char* nameStr = (char*)sqlite3_column_name(stmt, i); string name; if(nameStr != NULL) { name = nameStr; } else { char temp[32]; sprintf(temp, "tempCol%d", i); name = temp; } const char* typeStr = sqlite3_column_decltype(stmt, i); string type = typeStr != NULL ? typeStr : "int"; DataColumn* column = new DataColumn(name, type); table.addColumn(column); } while(sqlite3_step(stmt) == SQLITE_ROW) { DataRow* row = new DataRow(); for (int i = 0; i < columnCount; i++) { DataColumn* column = table.getColumns()->at(i); DataCell* cell = NULL; Value value; memset(&value, 0, sizeof(value)); ValueTypes type = column->getType(); switch (type) { case Null: break; case Integer: value.nValue = sqlite3_column_int(stmt, i); break; case String: case DateTime: { char* str = (char*)sqlite3_column_text(stmt, i); DataCell::setStringValue(value, str); } break; case Float: value.dValue = sqlite3_column_double(stmt, i); break; default: assert(false); break; } cell = new DataCell(column, value); row->addCell(cell); } table.addRow(row); } result = sqlite3_finalize(stmt); if(result != SQLITE_OK) { printErrorInfo("sqlite3_finalize", sql); return result; } return SQLITE_OK; }
int SqliteClient::executeSqlInsert(const DataTable& table) { Locker locker(&_sqliteDbMutex); if(table.getName().empty()) return -1; if(table.columnCount() == 0) return -2; if(table.rowCount() == 0) return -3; // such like '"INSERT INTO example VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)";' string valuesStr = ""; int columnCount = table.columnCount(); for (int i = 0; i < columnCount; i++) { if(i != 0) { valuesStr += ", "; } valuesStr += Convert::convertStr("?%d", i + 1); } string sql = Convert::convertStr("INSERT INTO [%s] VALUES (%s)", table.getName().c_str(), valuesStr.c_str()); sqlite3_stmt *stmt; int result = sqlite3_prepare_v2(_sqliteDb, sql.c_str(), sql.length(), &stmt, 0); if(result != SQLITE_OK) { printErrorInfo("sqlite3_prepare_v2", sql); return result; } result = execute("BEGIN TRANSACTION"); if(result != SQLITE_OK) { return result; } int rowCount = table.rowCount(); for (int i = 0; i < rowCount; i++) { const DataRow* row = table.getRows()->at(i); for (int j = 0; j < columnCount; j++) { const DataCell* cell = row->getCells()->at(j); if(cell != NULL) { ValueTypes type = cell->getType(); const Value value = cell->getValue(); switch (type) { case Null: sqlite3_bind_null(stmt, j + 1); break; case Integer: sqlite3_bind_int(stmt, j + 1, value.nValue); break; case DateTime: case String: result = sqlite3_bind_text(stmt, j + 1, value.strValue, strlen(value.strValue), SQLITE_TRANSIENT); break; case Float: sqlite3_bind_double(stmt, j + 1, value.dValue); break; default: assert(false); break; } } } result = sqlite3_step(stmt); if (result != SQLITE_DONE) { printErrorInfo("sqlite3_step", sql); } result = sqlite3_reset(stmt); if(result != SQLITE_OK) { printErrorInfo("sqlite3_reset", sql); } } result = execute("COMMIT TRANSACTION"); if(result != SQLITE_OK) { return result; } sqlite3_finalize(stmt); if(result != SQLITE_OK) { printErrorInfo("sqlite3_finalize", sql); return result; } return SQLITE_OK; }