//static kconn_t *MYSQL_qopen(CTX ctx, kbytes_t url) kconn_t *MYSQL_qopen(CTX ctx, kbytes_t url) { char *puser, user[MYSQL_USER_MAXLEN+1] = {0}; char *ppass, pass[MYSQL_PASS_MAXLEN+1] = {0}; // temporary defined char *phost, host[MYSQL_HOST_MAXLEN+1] = {0}; unsigned int port = 0; char *pdbnm, dbnm[MYSQL_DBNM_MAXLEN+1] = {0}; kbytes_t bt = knh_bytes_last(url, 8); // skip: mysql:// const char *btstr = bt.text; sscanf(btstr, "%16[^ :\r\n\t]:%255[^ @\r\n\t]@%255[^ :\r\n\t]:%5d/%64[^ \r\n\t]", (char*)&user, (char*)&pass, (char*)&host, &port, (char*)&dbnm); // consider to buffer over run puser = (user[0]) ? user : NULL; ppass = (pass[0]) ? pass : NULL; phost = (host[0]) ? host : NULL; pdbnm = (dbnm[0]) ? dbnm : NULL; MYSQL *db = mysql_init(NULL); KNH_NTRACE2(ctx, "mysql_init", (db != NULL) ? K_OK : K_FAILED, KNH_LDATA(LOG_s("init", mysql_error(db)))); db = mysql_real_connect(db, phost, puser, ppass, pdbnm, port, NULL, 0); KNH_NTRACE2(ctx, "mysql_real_connect", (db != NULL) ? K_OK : K_FAILED, KNH_LDATA(LOG_s("host", phost), LOG_s("user", puser), /* LOG_s("passwd", ppass), */ LOG_s("dbname", pdbnm), LOG_i("port", port), LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)))); //if (!mysql_real_connect(db, phost, puser, ppass, pdbnm, port, NULL, 0)) { // knh_mysql_perror(ctx, db, 0); // mysql_close(db); // db = NULL; //} return (kconn_t*)db; }
//static int MYSQL_qnext(CTX ctx, kqcur_t *qcur, struct kResultSet *rs) int MYSQL_qnext(CTX ctx, kqcur_t *qcur, struct kResultSet *rs) { MYSQL_ROW row; if ((row = mysql_fetch_row((MYSQL_RES*)qcur)) != NULL) { KNH_NTRACE2(ctx, "mysql_fetch_row", K_OK, KNH_LDATA0); int i; kint_t ival; kfloat_t fval; for (i = 0; i < DP(rs)->column_size; i++) { if (row[i] == NULL) { ResultSet_setNULL(ctx, rs, i); continue; } switch (DP(rs)->column[i].dbtype) { case MYSQL_TYPE_TINY: case MYSQL_TYPE_SHORT: case MYSQL_TYPE_INT24: case MYSQL_TYPE_LONG: case MYSQL_TYPE_LONGLONG: case MYSQL_TYPE_YEAR: knh_bytes_parseint(B(row[i]), &ival); ResultSet_setInt(ctx, rs, i, ival); break; case MYSQL_TYPE_FLOAT: case MYSQL_TYPE_DOUBLE: knh_bytes_parsefloat(B(row[i]), &fval); ResultSet_setFloat(ctx, rs, i, fval); break; case MYSQL_TYPE_NEWDECIMAL: case MYSQL_TYPE_STRING: case MYSQL_TYPE_VAR_STRING: case MYSQL_TYPE_TINY_BLOB: case MYSQL_TYPE_BLOB: case MYSQL_TYPE_MEDIUM_BLOB: case MYSQL_TYPE_LONG_BLOB: case MYSQL_TYPE_BIT: case MYSQL_TYPE_TIME: case MYSQL_TYPE_DATE: case MYSQL_TYPE_DATETIME: case MYSQL_TYPE_TIMESTAMP: ResultSet_setText(ctx, rs, i, B(row[i])); break; case MYSQL_TYPE_NULL: default: //KNH_SYSLOG(ctx, LOG_WARNING, "mysql", "mysql_qnext(dbtype)='unknown datatype [%d]'", DP(rs)->column[i].dbtype); ResultSet_setNULL(ctx, rs, i); break; } } /* for */ return 1; /* CONTINUE */ } else { KNH_NTRACE2(ctx, "mysql_fetch_row", K_FAILED, KNH_LDATA0); } return 0; /* NOMORE */ }
static kcontext_t* new_hcontext(CTX ctx0) { kcontext_t *ctx; static volatile size_t ctxid_counter = 0; if(ctx0 == NULL) { ctxid_counter = 0; ctx = (kcontext_t*)malloc(sizeof(kcontext_t)); knh_bzero(ctx, sizeof(kcontext_t)); } else { KNH_ASSERT_CTX0(ctx0); ctx = (kcontext_t*)KNH_MALLOC(ctx0, sizeof(kcontext_t)); knh_bzero(ctx, sizeof(kcontext_t)); } ctx->flag = 0; ctx->ctxid = ctxid_counter; ctxid_counter++; // ctx->freeObjectList = NULL; // ctx->freeMemoryList = NULL; ctx->parent = ctx; ctx->api2 = getapi2(); { kuintptr_t i = 0, ch; kuint_t t = knh_rand(); ch = t % 26; ctx->trace[i] = 'A' + ch; for(i = 1; i < 9; i++) { t = t / 36; if (t == 0) t = knh_rand(); ch = t % 36; ctx->trace[i] = (ch < 10) ? '0' + ch : 'A' + (ch - 10); } } ctx->seq = 0; ctx->ctxobjNC = NULL; if(ctx0 == NULL) { const char *ptrace = knh_getenv(K_DEOS_TRACE); if(ptrace == NULL) { ptrace = "$(setenv " K_DEOS_TRACE ")"; } KNH_NTRACE2(ctx, "konoha:newtrace", K_NOTICE, KNH_LDATA( LOG_s("parent", ptrace) #if defined(K_USING_POSIX_) , LOG_u("ppid", getppid()) #endif /* !defined(K_USING_POSIX_) */ )); } else { KNH_NTRACE2(ctx, "konoha:newtrace", K_NOTICE, KNH_LDATA(LOG_s("parent", ctx0->trace), LOG_u("seq", ctx0->seq))); } return ctx; }
//static kqcur_t *MYSQL_query(CTX ctx, kconn_t *hdr, kbytes_t sql, kResultSet *rs) kqcur_t *MYSQL_query(CTX ctx, kconn_t *hdr, kbytes_t sql, kResultSet *rs) { MYSQL_RES *res = NULL; MYSQL *db = (MYSQL*)hdr; if (db == NULL) { /* return NULL */ } else if (rs == NULL) { /* Connection.exec */ int r = mysql_query(db, sql.text); KNH_NTRACE2(ctx, "mysql_query", (r == 0) ? K_OK : K_FAILED, KNH_LDATA(LOG_s("query", sql.text), LOG_i("errno", mysql_errno(db)))); //if(r > 0) { // knh_mysql_perror(ctx, (MYSQL*)db, r); //} } else { /* Connection.query */ int r = mysql_query((MYSQL*)db, sql.text); KNH_NTRACE2(ctx, "mysql_query", (r == 0) ? K_OK : K_FAILED, KNH_LDATA( LOG_s("query", sql.text), LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)) )); if (r == 0) { res = mysql_store_result((MYSQL*)db); if (res == NULL) { // NULL RESULT if (mysql_errno(db) != 0) { mysql_free_result(res); KNH_NTRACE2(ctx, "mysql_store_result", K_FAILED, KNH_LDATA(LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)))); } else { KNH_NTRACE2(ctx, "mysql_store_result", K_OK, KNH_LDATA(LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)))); } } else { knh_ResultSet_initColumn(ctx, rs, (size_t)mysql_num_fields(res)); KNH_NTRACE2(ctx, "mysql_store_result", K_OK, KNH_LDATA(LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)))); int i = 0; MYSQL_FIELD *field = NULL; while((field = mysql_fetch_field(res))) { DP(rs)->column[i].dbtype = field->type; kString *s = new_String(ctx, field->name); ResultSet_setName(ctx, rs, i, s); i++; } } } } return (kqcur_t *) res; }
void *knh_dlsym(CTX, void* handler, const char* symbol, const char *another, int isTest) { const char *func = __FUNCTION__, *emsg = NULL; void *p = NULL; #if defined(K_USING_WINDOWS_) func = "GetProcAddress"; p = GetProcAddress((HMODULE)handler, (LPCSTR)symbol); if(p == NULL && another != NULL) { symbol = another; p = GetProcAddress((HMODULE)handler, (LPCSTR)symbol); } return p; #elif defined(K_USING_POSIX_) func = "dlsym"; p = dlsym(handler, symbol); if(p == NULL && another != NULL) { symbol = another; p = dlsym(handler, symbol); } if(p == NULL) { emsg = dlerror(); } #else #endif if(!isTest) { int pe = (p == NULL) ? K_PERROR : K_OK; KNH_NTRACE2(_ctx, func, pe, KNH_LDATA(LOG_p("handler", handler), LOG_s("symbol", symbol))); } return p; }
void knh_mutex_free(CTX, kmutex_t *m) { if(knh_mutex_destroy(m) != 0) { KNH_NTRACE2(_ctx, "mutex_destroy", K_PERROR, KNH_LDATA(LOG_p("mutex", m))); } free(m); }
kmutex_t *knh_mutex_malloc(CTX) { kmutex_t *m = (kmutex_t*)malloc(sizeof(kmutex_t)); knh_bzero(m, sizeof(kmutex_t)); if(knh_mutex_init(m) != 0) { KNH_NTRACE2(_ctx, "mutex_init", K_PERROR, KNH_LDATA(LOG_p("mutex", m))); } return m; }
kfileid_t knh_getURI(CTX, kbytes_t t) { OLD_LOCK(_ctx, LOCK_SYSTBL, NULL); kindex_t idx = knh_DictSet_index(_ctx->share->urnDictSet, t); if(idx == -1) { kString *s = new_kString(t.text, t.len, SPOL_POOL); idx = kArray_size(_ctx->share->urns); knh_DictSet_set(_ctx, ctx->share->urnDictSet, s, idx); kArray_add(ctx->share->urns, s); KNH_NTRACE2(_ctx, "konoha:newfileid", K_OK, KNH_LDATA(LOG_s("urn", S_text(s)), LOG_i("fileid", idx))); } else { idx = knh_DictSet_valueAt(_ctx->share->urnDictSet, idx); } OLD_UNLOCK(_ctx, LOCK_SYSTBL, NULL); return (kfileid_t)idx; }
kuri_t knh_getURI(CTX ctx, kbytes_t t) { OLD_LOCK(ctx, LOCK_SYSTBL, NULL); kindex_t idx = knh_DictSet_index(ctx->share->urnDictSet, t); if(idx == -1) { kString *s = new_String2(ctx, CLASS_String, t.text, t.len, SPOL_POOLALWAYS); idx = knh_Array_size(ctx->share->urns); knh_DictSet_set(ctx, ctx->share->urnDictSet, s, idx); knh_Array_add(ctx, ctx->share->urns, s); KNH_NTRACE2(ctx, "konoha:newuri", K_OK, KNH_LDATA(LOG_s("urn", S_totext(s)), LOG_i("uri", idx))); } else { idx = knh_DictSet_valueAt(ctx->share->urnDictSet, idx); } OLD_UNLOCK(ctx, LOCK_SYSTBL, NULL); return (kuri_t)idx; }
void *knh_dlopen(CTX, const char* path) { const char *func = __FUNCTION__; void *handler = NULL; #if defined(K_USING_WINDOWS_) func = "LoadLibrary"; handler = (void*)LoadLibraryA((LPCTSTR)path); #elif defined(K_USING_POSIX_) func = "dlopen"; handler = dlopen(path, RTLD_LAZY); #else #endif int pe = (handler == NULL) ? K_PERROR : K_OK; { KNH_NTRACE2(_ctx, func, pe, KNH_LDATA(LOG_s("path", path), LOG_p("handler", handler))); } return handler; }
static int open_socket(CTX ctx, ksfp_t *sfp, const char *ip_or_host, int port) { int sd = IO_NULL; struct in_addr addr = {0}; struct sockaddr_in server = {0}; const char *errfunc = NULL; if ((addr.s_addr = inet_addr(ip_or_host)) == -1) { struct hostent *host = gethostbyname(ip_or_host); if (host == NULL) { errfunc = "gethostname"; goto L_PERROR; } memcpy(&addr, (struct in_addr *)*host->h_addr_list, sizeof(struct in_addr)); } server.sin_family = AF_INET; server.sin_addr = addr; server.sin_port = htons(port); if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { errfunc = "socket"; goto L_PERROR; } if (connect(sd, (struct sockaddr *)&server, sizeof(server)) == -1) { errfunc = "connect"; close(sd); goto L_PERROR; } L_PERROR:; if (errfunc != NULL) { KNH_NTHROW2(ctx, sfp, "Socket!!", errfunc, K_PERROR, KNH_LDATA(LOG_s("host", ip_or_host), LOG_i("port", port), LOG_sfp)); sd = IO_NULL; } else { KNH_NTRACE2(ctx, "connect", K_OK, KNH_LDATA(LOG_s("host", ip_or_host), LOG_i("port", port), LOG_sfp)); } return (int) sd; }
static void trapSIGINT(int sig RECARG) { CTX ctx = knh_getCurrentContext(); // record_signal(ctx, sig RECDATA); if(ctx != NULL) { //#if defined(K_USING_MINGW_) // knh_ldata_t ldata[] = {LOG_END}; //#else // knh_ldata_t ldata[] = {LOG_i("sender_pid", si->si_pid), LOG_i("sender_uid", si->si_uid), LOG_END}; //#endif /* defined(K_USING_MINGW_) */ // KNH_NTRACE(ctx, "konoha:signal", K_NOTICE, ldata); KNH_NTRACE2(ctx, "konoha:signal", K_NOTICE, #if defined(K_USING_MINGW_) KNH_LDATA0 #else KNH_LDATA(LOG_i("sender_pid", si->si_pid), LOG_i("sender_uid", si->si_uid)) #endif ); } _Exit(0); }
int main(int argc, const char *argv[]) { MPI_Init(&argc, (char***)&argv); konoha_ginit(argc, argv); konoha_t konoha = konoha_open(); int _world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &_world_rank); #ifdef KNH_MPI_PROFILE double _begin = MPI_Wtime(); #endif konoha_main(konoha, argc, argv); #ifdef KNH_MPI_PROFILE double _finish = MPI_Wtime(); double _duration = _finish - _begin; { CTX ctx = (CTX)konoha; KNH_NTRACE2(ctx, "konoha_main(MPI)", K_NOTICE, KNH_LDATA(LOG_f("begin", _begin), LOG_f("finish", _finish), LOG_f("duration", _duration), LOG_i("myrank", _world_rank))); } #endif konoha_close(konoha); MPI_Finalize(); return 0; }