static VALUE rb_git_indexentry_fromC(const git_index_entry *entry) { VALUE rb_entry, rb_mtime, rb_ctime; unsigned int valid, stage; if (!entry) return Qnil; rb_entry = rb_hash_new(); rb_hash_aset(rb_entry, CSTR2SYM("path"), rb_str_new_utf8(entry->path)); rb_hash_aset(rb_entry, CSTR2SYM("oid"), rugged_create_oid(&entry->id)); rb_hash_aset(rb_entry, CSTR2SYM("dev"), INT2FIX(entry->dev)); rb_hash_aset(rb_entry, CSTR2SYM("ino"), INT2FIX(entry->ino)); rb_hash_aset(rb_entry, CSTR2SYM("mode"), INT2FIX(entry->mode)); rb_hash_aset(rb_entry, CSTR2SYM("gid"), INT2FIX(entry->gid)); rb_hash_aset(rb_entry, CSTR2SYM("uid"), INT2FIX(entry->uid)); rb_hash_aset(rb_entry, CSTR2SYM("file_size"), INT2FIX(entry->file_size)); valid = (entry->flags & GIT_IDXENTRY_VALID); rb_hash_aset(rb_entry, CSTR2SYM("valid"), valid ? Qtrue : Qfalse); stage = (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT; rb_hash_aset(rb_entry, CSTR2SYM("stage"), INT2FIX(stage)); rb_mtime = rb_time_new(entry->mtime.seconds, entry->mtime.nanoseconds / 1000); rb_ctime = rb_time_new(entry->ctime.seconds, entry->ctime.nanoseconds / 1000); rb_hash_aset(rb_entry, CSTR2SYM("ctime"), rb_ctime); rb_hash_aset(rb_entry, CSTR2SYM("mtime"), rb_mtime); return rb_entry; }
static VALUE parse_double_time(const char *text) { long v = 0; long v2 = 0; const char *dot = 0; char c; for (; '.' != *text; text++) { c = *text; if (c < '0' || '9' < c) { return Qnil; } v = 10 * v + (long)(c - '0'); } dot = text++; for (; '\0' != *text && text - dot <= 6; text++) { c = *text; if (c < '0' || '9' < c) { return Qnil; } v2 = 10 * v2 + (long)(c - '0'); } for (; text - dot <= 9; text++) { v2 *= 10; } #if HAS_NANO_TIME return rb_time_nano_new(v, v2); #else return rb_time_new(v, v2 / 1000); #endif }
static VALUE t_get_loop_time (VALUE self) { if (gCurrentLoopTime != 0) { return rb_time_new(gCurrentLoopTime / 1000000, gCurrentLoopTime % 1000000); } return Qnil; }
static VALUE rrrd_rrd_last_update(VALUE self) { list arguments = rrrd_list_new(); rrrd_add_filename_arg(self, &arguments); time_t last_update; unsigned long ds_count; char **raw_names, **last_ds; rrd_lastupdate(arguments.length, arguments.strings, &last_update, &ds_count, &raw_names, &last_ds); rrrd_free_arguments(arguments); rrrd_check_for_errors(); VALUE timestamp = rb_time_new(last_update, 0); VALUE values = rb_hash_new(); int i; for (i = 0; i < ds_count; i++) { VALUE key = ID2SYM(rb_intern(raw_names[i])); VALUE value = rb_funcall(rb_str_new2(last_ds[i]), rb_intern("to_f"), 0); rb_hash_aset(values, key, value); } VALUE args[] = { timestamp, values }; VALUE tuple = rb_class_new_instance(2, args, Tuple); return tuple; }
static VALUE des0_read_time(AMF_DESERIALIZER *des) { double milli = des_read_double(des); des_read_uint16(des); // Timezone - unused time_t sec = milli/1000.0; time_t micro = (milli-sec*1000)*1000; return rb_time_new(sec, micro); }
VALUE rugged_signature_new(const git_signature *sig, const char *encoding_name) { VALUE rb_sig, rb_time; rb_encoding *encoding = rb_utf8_encoding(); if (encoding_name != NULL) encoding = rb_enc_find(encoding_name); rb_sig = rb_hash_new(); /* Allocate the time with a the given timezone */ rb_time = rb_funcall( rb_time_new(sig->when.time, 0), rb_intern("getlocal"), 1, INT2FIX(sig->when.offset * 60) ); rb_hash_aset(rb_sig, CSTR2SYM("name"), rb_enc_str_new(sig->name, strlen(sig->name), encoding)); rb_hash_aset(rb_sig, CSTR2SYM("email"), rb_enc_str_new(sig->email, strlen(sig->email), encoding)); rb_hash_aset(rb_sig, CSTR2SYM("time"), rb_time); return rb_sig; }
static VALUE des0_read_time(VALUE self) { AMF_DESERIALIZER *des; Data_Get_Struct(self, AMF_DESERIALIZER, des); double milli = des_read_double(des); des_read_uint16(des); // Timezone - unused time_t sec = milli/1000.0; time_t micro = (milli-sec*1000)*1000; return rb_time_new(sec, micro); }
static VALUE rrrd_rrd_last(VALUE self) { list arguments = rrrd_list_new(); rrrd_add_filename_arg(self, &arguments); time_t last = rrd_last(arguments.length, arguments.strings); rrrd_free_arguments(arguments); rrrd_check_for_errors(); return rb_time_new(last, 0); }
STATIC VALUE rb_read_date(ramf0_load_context_t* context) { double seconds_f = c_read_double(context) / 1000.0; uint16_t tz = c_read_word16_network(context); time_t seconds = seconds_f; time_t microseconds = (seconds_f - (double)seconds) * (1000.0 * 1000.0); return rb_time_new(seconds, microseconds); }
VALUE method_boot_time(VALUE self) { struct timeval tv; size_t size = sizeof(tv); static int which[] = { CTL_KERN, KERN_BOOTTIME }; if (sysctl(which, 2, &tv, &size, NULL, 0) == 0) { return rb_time_new(tv.tv_sec, tv.tv_usec); } else { return Qnil; } }
static VALUE bf_get_visited(VALUE self, VALUE uri) { GError *error = NULL; time_t ret = g_bookmark_file_get_visited(_SELF(self), (const gchar *)RVAL2CSTR(uri), &error); if (!ret) RAISE_GERROR(error); return rb_time_new(ret, 0); }
static void data_objects_debug(VALUE connection, VALUE string, struct timeval* start) { struct timeval stop; VALUE message; gettimeofday(&stop, NULL); do_int64 duration = (stop.tv_sec - start->tv_sec) * 1000000 + stop.tv_usec - start->tv_usec; message = rb_funcall(cDO_Logger_Message, DO_ID_NEW, 3, string, rb_time_new(start->tv_sec, start->tv_usec), INT2NUM(duration)); rb_funcall(connection, DO_ID_LOG, 1, message); }
static VALUE parse_xsd_time(const char *text) { long cargs[10]; long *cp = cargs; long v; int i; char c = '\0'; struct _tp tpa[10] = { { 4, '-', '-' }, { 2, '-', '-' }, { 2, 'T', ' ' }, { 2, ':', ':' }, { 2, ':', ':' }, { 2, '.', '.' }, { 9, '+', '-' }, { 2, ':', ':' }, { 2, '\0', '\0' }, { 0, '\0', '\0' } }; Tp tp = tpa; struct tm tm; memset(cargs, 0, sizeof(cargs)); for (; 0 != tp->cnt; tp++) { for (i = tp->cnt, v = 0; 0 < i ; text++, i--) { c = *text; if (c < '0' || '9' < c) { if ('\0' == c || tp->end == c || tp->alt == c) { break; } return Qnil; } v = 10 * v + (long)(c - '0'); } if ('\0' == c) { break; } c = *text++; if (tp->end != c && tp->alt != c) { return Qnil; } *cp++ = v; } tm.tm_year = (int)cargs[0] - 1900; tm.tm_mon = (int)cargs[1] - 1; tm.tm_mday = (int)cargs[2]; tm.tm_hour = (int)cargs[3]; tm.tm_min = (int)cargs[4]; tm.tm_sec = (int)cargs[5]; #if HAS_NANO_TIME return rb_time_nano_new(mktime(&tm), cargs[6]); #else return rb_time_new(mktime(&tm), cargs[6] / 1000); #endif }
VALUE occurrences( VALUE self, VALUE dtstart, VALUE dtend, VALUE rrule ) { char * _rrule; struct icaltimetype start, end; time_t tt; VALUE tv_sec, occurr = rb_ary_new(); /* Get method ID for Time.tv_sec */ ID time_tv_sec = rb_intern( "tv_sec" ); ID to_string = rb_intern( "to_string" ); if( TYPE( rrule ) != T_STRING && rb_respond_to( rrule, to_string ) ) rrule = rb_funcall( rrule, to_string, 0 ); Check_Type(rrule, T_STRING); _rrule = RSTRING(rrule)->ptr; dtstart = to_time( dtstart, "dtstart" ); dtend = to_time( dtend, "dtend" ); /* Apply .tv_sec to our Time objects (if they are Times ...) */ tv_sec = rb_funcall( dtstart, time_tv_sec, 0 ); tt = NUM2INT( tv_sec ); start = icaltime_from_timet( tt, 0 ); tv_sec = rb_funcall( dtend, time_tv_sec, 0 ); tt = NUM2INT( tv_sec ); end = icaltime_from_timet( tt, 0 ); icalerror_clear_errno(); icalerror_set_error_state( ICAL_MALFORMEDDATA_ERROR, ICAL_ERROR_NONFATAL); struct icalrecurrencetype recur = icalrecurrencetype_from_string( _rrule ); if( icalerrno != ICAL_NO_ERROR ) { rb_raise(rb_eArgError, "Malformed RRule"); return Qnil; } icalrecur_iterator* ritr = icalrecur_iterator_new( recur, start ); while(1) { struct icaltimetype next = icalrecur_iterator_next(ritr); if( icaltime_is_null_time(next) || ( icaltime_compare( next, end ) > 0 ) ) { icalrecur_iterator_free(ritr); return occurr; } rb_ary_push( occurr, rb_time_new( icaltime_as_timet( next ), 0 ) ); }; icalrecur_iterator_free(ritr); return occurr; }
static VALUE t_get_loop_time (VALUE self UNUSED) { uint64_t current_time = evma_get_current_loop_time(); if (current_time == 0) { return Qnil; } // Generally the industry has moved to 64-bit time_t, this is just in case we're 32-bit time_t. if (sizeof(time_t) < 8 && current_time > INT_MAX) { return rb_funcall(rb_cTime, Intern_at, 2, INT2NUM(current_time / 1000000), INT2NUM(current_time % 1000000)); } else { return rb_time_new(current_time / 1000000, current_time % 1000000); } }
static VALUE bf_get_app_info(VALUE self, VALUE uri, VALUE name) { gchar* exec; guint count; time_t stamp; GError* error = NULL; gboolean ret = g_bookmark_file_get_app_info(_SELF(self), (const gchar *)RVAL2CSTR(uri), (const gchar *)RVAL2CSTR(name), &exec, &count, &stamp, &error); if (!ret) RAISE_GERROR(error); return rb_ary_new3(3, CSTR2RVAL(exec), UINT2NUM(count), rb_time_new(stamp, 0)); }
static VALUE t_get_loop_time (VALUE self) { #ifndef HAVE_RB_TIME_NEW static VALUE cTime = rb_path2class("Time"); static ID at = rb_intern("at"); #endif uint64_t current_time = evma_get_current_loop_time(); if (current_time != 0) { #ifndef HAVE_RB_TIME_NEW return rb_funcall(cTime, at, 2, INT2NUM(current_time / 1000000), INT2NUM(current_time % 1000000)); #else return rb_time_new(current_time / 1000000, current_time % 1000000); #endif } return Qnil; }
static void cf_hash_to_rb_hash(const void *raw_key, const void * raw_value, void *ctx){ CFTypeRef value = (CFTypeRef)raw_value; CFStringRef key = (CFStringRef)raw_key; VALUE rubyValue = Qnil; VALUE hash = (VALUE)ctx; if(CFStringGetTypeID() == CFGetTypeID(value)){ rubyValue = cfstring_to_rb_string((CFStringRef)value); } else if(CFDataGetTypeID() == CFGetTypeID(value)){ CFDataRef data = (CFDataRef)value; rubyValue = rb_enc_str_new((const char*)CFDataGetBytePtr(data),CFDataGetLength(data), rb_ascii8bit_encoding()); } else if(CFBooleanGetTypeID() == CFGetTypeID(value)){ Boolean booleanValue = CFBooleanGetValue(value); rubyValue = booleanValue ? Qtrue : Qfalse; } else if(CFNumberGetTypeID() == CFGetTypeID(value)){ if(CFNumberIsFloatType(value)) { double doubleValue; CFNumberGetValue(value, kCFNumberDoubleType, &doubleValue); rubyValue = rb_float_new(doubleValue); }else{ long long longValue; CFNumberGetValue(value, kCFNumberLongLongType, &longValue); rubyValue = LL2NUM(longValue); } } else if (CFDateGetTypeID() == CFGetTypeID(value)){ CFDateRef date = (CFDateRef) value; CFAbsoluteTime abs_time = CFDateGetAbsoluteTime(date); double secondsSinceUnixEpoch = abs_time + kCFAbsoluteTimeIntervalSince1970; time_t seconds = (time_t)secondsSinceUnixEpoch; long usec = (secondsSinceUnixEpoch - seconds) * 1000000; rubyValue = rb_time_new((time_t)secondsSinceUnixEpoch, usec); } if(!NIL_P(rubyValue)){ rb_hash_aset(hash, cfstring_to_rb_string(key), rubyValue); } }
static VALUE rb_rcsrev_new(struct rcsrev *rev) { struct tm tm; const char *month; VALUE self; VALUE date; memset(&tm, 0, sizeof(tm)); if (rev->date->len == 17) { /* 2-digit year */ readdate(rev->date->str, &tm.tm_year, 2); month = rev->date->str + 3; } else { /* 4-digit year */ readdate(rev->date->str, &tm.tm_year, 4); tm.tm_year -= 1900; month = rev->date->str + 5; } readdate(month, &tm.tm_mon, 2); tm.tm_mon--; readdate(month + 3, &tm.tm_mday, 2); readdate(month + 6, &tm.tm_hour, 2); readdate(month + 9, &tm.tm_min, 2); readdate(month + 12, &tm.tm_sec, 2); date = rb_time_new(timegm(&tm), 0); /* * rb_time_new returns a Time object in local time, so convert * it to GMT, what RCS/CVS uses everywhere. */ date = rb_funcall(date, rb_intern("gmtime"), 0); self = rb_obj_alloc(rb_cRev); rb_iv_set(self, "@rev", str_from_tok(rev->rev)); rb_iv_set(self, "@date", date); rb_iv_set(self, "@author", str_from_tok(rev->author)); rb_iv_set(self, "@state", str_from_tok2(rev->state)); rb_iv_set(self, "@branches", ary_from_toklist(&rev->branches)); rb_iv_set(self, "@next", str_from_tok2(rev->next)); rb_iv_set(self, "@commitid", str_from_tok2(rev->commitid)); return self; }
static VALUE rrrd_rrd_first(int argc, VALUE *argv, VALUE self) { VALUE index; rb_scan_args(argc, argv, "01", &index); list arguments = rrrd_list_new(); rrrd_add_filename_arg(self, &arguments); // RRA index if (RTEST(index)) { index = rb_fix2str(index, 10); rrrd_add_arg(&arguments, "--rraindex"); rrrd_add_arg(&arguments, STR2CSTR(index)); } time_t first = rrd_first(arguments.length, arguments.strings); rrrd_free_arguments(arguments); rrrd_check_for_errors(); return rb_time_new(first, 0); }
VALUE rugged_signature_new(const git_signature *sig, const char *encoding_name) { VALUE rb_sig, rb_time; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *encoding = NULL; if (encoding_name != NULL) encoding = rb_enc_find(encoding_name); #endif rb_sig = rb_hash_new(); rb_time = rb_time_new(sig->when.time, 0); rb_funcall(rb_time, rb_intern("utc"), 0); rb_hash_aset(rb_sig, CSTR2SYM("name"), rugged_str_new2(sig->name, encoding)); rb_hash_aset(rb_sig, CSTR2SYM("email"), rugged_str_new2(sig->email, encoding)); rb_hash_aset(rb_sig, CSTR2SYM("time"), rb_time); return rb_sig; }
static VALUE typecast_timestamp(const char *data, uint64_t len) { struct tm tm; int64_t epoch, adjust, offset; char tzsign = 0; int tzhour = 0, tzmin = 0; long double sec_fraction = 0; memset(&tm, 0, sizeof(struct tm)); if (strchr(data, '.')) { sscanf(data, "%04d-%02d-%02d %02d:%02d:%02d%Lf%c%02d:%02d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &sec_fraction, &tzsign, &tzhour, &tzmin); } else { sscanf(data, "%04d-%02d-%02d %02d:%02d:%02d%c%02d:%02d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tzsign, &tzhour, &tzmin); } tm.tm_year -= 1900; tm.tm_mon -= 1; tm.tm_isdst = -1; if (tm.tm_mday > 0) { epoch = mktime(&tm); adjust = client_tzoffset(epoch, tm.tm_isdst); offset = adjust; if (tzsign) { offset = tzsign == '+' ? (time_t)tzhour * 3600 + (time_t)tzmin * 60 : (time_t)tzhour * -3600 + (time_t)tzmin * -60; } return rb_time_new(epoch+adjust-offset, (uint64_t)(sec_fraction*1000000L)); } return rb_str_new(data, len); }
VALUE addTimeToHash(VALUE hash, const char* key, time_t val) { return rb_hash_aset(hash, rb_str_new2(key), rb_time_new(val,0)); }
VALUE rho_ruby_create_time(long t) { return rb_time_new(t,0); }
static VALUE get_value(const char* buffer, int* position, int type) { VALUE value; switch (type) { case -1: { value = rb_class_new_instance(0, NULL, MinKey); break; } case 1: { double d; memcpy(&d, buffer + *position, 8); value = rb_float_new(d); *position += 8; break; } case 2: case 13: { int value_length; value_length = *(int*)(buffer + *position) - 1; *position += 4; value = STR_NEW(buffer + *position, value_length); *position += value_length + 1; break; } case 3: { int size; memcpy(&size, buffer + *position, 4); if (strcmp(buffer + *position + 5, "$ref") == 0) { // DBRef int offset = *position + 10; VALUE argv[2]; int collection_length = *(int*)(buffer + offset) - 1; char id_type; offset += 4; argv[0] = STR_NEW(buffer + offset, collection_length); offset += collection_length + 1; id_type = buffer[offset]; offset += 5; argv[1] = get_value(buffer, &offset, (int)id_type); value = rb_class_new_instance(2, argv, DBRef); } else { value = elements_to_hash(buffer + *position + 4, size - 5); } *position += size; break; } case 4: { int size, end; memcpy(&size, buffer + *position, 4); end = *position + size - 1; *position += 4; value = rb_ary_new(); while (*position < end) { int type = (int)buffer[(*position)++]; int key_size = (int)strlen(buffer + *position); VALUE to_append; *position += key_size + 1; // just skip the key, they're in order. to_append = get_value(buffer, position, type); rb_ary_push(value, to_append); } (*position)++; break; } case 5: { int length, subtype; VALUE data, st; VALUE argv[2]; memcpy(&length, buffer + *position, 4); subtype = (unsigned char)buffer[*position + 4]; if (subtype == 2) { data = rb_str_new(buffer + *position + 9, length - 4); } else { data = rb_str_new(buffer + *position + 5, length); } st = INT2FIX(subtype); argv[0] = data; argv[1] = st; value = rb_class_new_instance(2, argv, Binary); *position += length + 5; break; } case 6: { value = Qnil; break; } case 7: { VALUE str = rb_str_new(buffer + *position, 12); VALUE oid = rb_funcall(str, unpack_method, 1, rb_str_new2("C*")); value = rb_class_new_instance(1, &oid, ObjectId); *position += 12; break; } case 8: { value = buffer[(*position)++] ? Qtrue : Qfalse; break; } case 9: { long long millis; memcpy(&millis, buffer + *position, 8); value = rb_time_new(millis / 1000, (millis % 1000) * 1000); value = rb_funcall(value, utc_method, 0); *position += 8; break; } case 10: { value = Qnil; break; } case 11: { int pattern_length = (int)strlen(buffer + *position); VALUE pattern = STR_NEW(buffer + *position, pattern_length); int flags_length, flags = 0, i = 0; VALUE argv[3]; *position += pattern_length + 1; flags_length = (int)strlen(buffer + *position); for (i = 0; i < flags_length; i++) { char flag = buffer[*position + i]; if (flag == 'i') { flags |= IGNORECASE; } else if (flag == 'm') { flags |= MULTILINE; } else if (flag == 's') { flags |= MULTILINE; } else if (flag == 'x') { flags |= EXTENDED; } } argv[0] = pattern; argv[1] = INT2FIX(flags); value = rb_class_new_instance(2, argv, Regexp); *position += flags_length + 1; break; } case 12: { int collection_length; VALUE collection, str, oid, id, argv[2]; collection_length = *(int*)(buffer + *position) - 1; *position += 4; collection = STR_NEW(buffer + *position, collection_length); *position += collection_length + 1; str = rb_str_new(buffer + *position, 12); oid = rb_funcall(str, unpack_method, 1, rb_str_new2("C*")); id = rb_class_new_instance(1, &oid, ObjectId); *position += 12; argv[0] = collection; argv[1] = id; value = rb_class_new_instance(2, argv, DBRef); break; } case 14: { int value_length; memcpy(&value_length, buffer + *position, 4); value = ID2SYM(rb_intern(buffer + *position + 4)); *position += value_length + 4; break; } case 15: { int code_length, scope_size; VALUE code, scope, argv[2]; *position += 4; code_length = *(int*)(buffer + *position) - 1; *position += 4; code = STR_NEW(buffer + *position, code_length); *position += code_length + 1; memcpy(&scope_size, buffer + *position, 4); scope = elements_to_hash(buffer + *position + 4, scope_size - 5); *position += scope_size; argv[0] = code; argv[1] = scope; value = rb_class_new_instance(2, argv, Code); break; } case 16: { int i; memcpy(&i, buffer + *position, 4); value = LL2NUM(i); *position += 4; break; } case 17: { int sec, inc; VALUE argv[2]; memcpy(&inc, buffer + *position, 4); memcpy(&sec, buffer + *position + 4, 4); argv[0] = INT2FIX(sec); argv[1] = INT2FIX(inc); value = rb_class_new_instance(2, argv, Timestamp); *position += 8; break; } case 18: { long long ll; memcpy(&ll, buffer + *position, 8); value = LL2NUM(ll); *position += 8; break; } case 127: { value = rb_class_new_instance(0, NULL, MaxKey); break; } default: { rb_raise(rb_eTypeError, "no c decoder for this type yet (%d)", type); break; } } return value; }
/* * call-seq: * ProcTable.ps(pid=nil) * ProcTable.ps(pid=nil){ |ps| ... } * * In block form, yields a ProcTableStruct for each process entry that you * have rights to. This method returns an array of ProcTableStruct's in * non-block form. * * If a +pid+ is provided, then only a single ProcTableStruct is yielded or * returned, or nil if no process information is found for that +pid+. */ static VALUE pt_ps(int argc, VALUE* argv, VALUE klass){ int err; char state[8]; struct kinfo_proc* procs; VALUE v_pid, v_tty_num, v_tty_dev, v_start_time; VALUE v_pstruct = Qnil; VALUE v_array = rb_ary_new(); size_t length, count; size_t i = 0; char args[ARGS_MAX_LEN+1]; // Passed into sysctl call static const int name_mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; rb_scan_args(argc, argv, "01", &v_pid); // Get size of proc kproc buffer err = sysctl( (int *) name_mib, PROC_MIB_LEN, NULL, &length, NULL, 0); if(err == -1) rb_raise(cProcTableError, "sysctl: %s", strerror(errno)); // Populate the kproc buffer procs = malloc(length); if(procs == NULL) rb_raise(cProcTableError, "malloc: %s", strerror(errno)); err = sysctl( (int *) name_mib, PROC_MIB_LEN, procs, &length, NULL, 0); if(err == -1) rb_raise(cProcTableError, "sysctl: %s", strerror(errno)); // If we're here, we got our list count = length / sizeof(struct kinfo_proc); for(i = 0; i < count; i++) { v_tty_num = Qnil; v_tty_dev = Qnil; v_start_time = Qnil; // If a PID is provided, skip unless the PID matches if( (!NIL_P(v_pid)) && (procs[i].kp_proc.p_pid != NUM2INT(v_pid)) ) continue; *args = '\0'; /* Query the command line args */ /* TODO: Cmd line not working for now - fix */ /*args_mib[ARGS_MIB_LEN - 1] = procs[i].kp_proc.p_pid; args_err = sysctl( (int *) args_mib, ARGS_MIB_LEN, args, &args_size, NULL, 0); if(args_err >= 0) { fprintf(stderr, "Ret: %d LEN: %d\n", err, args_size); char *c; for(c = args; c < args+args_size; c++) if(*c == '\0') *c = ' '; args[args_size] = '\0'; } else { fprintf(stderr, "err: %s LEN: %d\n", strerror(errno), args_size); }*/ char cmdline[ARGS_MAX_LEN+1]; argv_of_pid(procs[i].kp_proc.p_pid, &cmdline); /* free(cmdline); */ // Get the start time of the process v_start_time = rb_time_new( procs[i].kp_proc.p_un.__p_starttime.tv_sec, procs[i].kp_proc.p_un.__p_starttime.tv_usec ); // Get the state of the process switch(procs[i].kp_proc.p_stat) { case SIDL: strcpy(state, "idle"); break; case SRUN: strcpy(state, "run"); break; case SSLEEP: strcpy(state, "sleep"); break; case SSTOP: strcpy(state, "stop"); break; case SZOMB: strcpy(state, "zombie"); break; default: strcpy(state, "unknown"); break; } // Get ttynum and ttydev. If ttynum is -1, there is no tty. if(procs[i].kp_eproc.e_tdev != -1){ v_tty_num = INT2FIX(procs[i].kp_eproc.e_tdev), v_tty_dev = rb_str_new2(devname(procs[i].kp_eproc.e_tdev, S_IFCHR)); } v_pstruct = rb_struct_new( sProcStruct, INT2FIX(procs[i].kp_proc.p_pid), INT2FIX(procs[i].kp_eproc.e_ppid), INT2FIX(procs[i].kp_eproc.e_pgid), INT2FIX(procs[i].kp_eproc.e_pcred.p_ruid), INT2FIX(procs[i].kp_eproc.e_pcred.p_rgid), rb_str_new2(procs[i].kp_proc.p_comm), rb_str_new2(state), rb_float_new(procs[i].kp_proc.p_pctcpu), Qnil, v_tty_num, v_tty_dev, rb_str_new2(procs[i].kp_eproc.e_wmesg), INT2FIX(procs[i].kp_proc.p_rtime.tv_sec), INT2FIX(procs[i].kp_proc.p_priority), INT2FIX(procs[i].kp_proc.p_usrpri), INT2FIX(procs[i].kp_proc.p_nice), rb_str_new2(cmdline), v_start_time, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_maxrss) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_ixrss) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_idrss) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_isrss) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_minflt) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_majflt) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_nswap) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_inblock) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_oublock) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_msgsnd) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_msgrcv) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_nsignals) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_nvcsw) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_nivcsw) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_utime.tv_sec) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_stime.tv_sec) : Qnil ); OBJ_FREEZE(v_pstruct); // This is read-only data if(rb_block_given_p()) rb_yield(v_pstruct); else rb_ary_push(v_array, v_pstruct); } if(procs) free(procs); if(!rb_block_given_p()){ if(NIL_P(v_pid)) return v_array; else return v_pstruct; } return Qnil; }
/** * Converts the raw BSON bytes into a UTC Ruby time. * * @example Convert the bytes to a Ruby time. * rb_time_from_bson(time, bytes); * * @param [ Class ] self The Ruby Time class. * @param [ String ] bytes The raw BSON bytes. * * @return [ Time ] The UTC time. * * @since 2.0.0 */ static VALUE rb_time_from_bson(VALUE self, VALUE bytes) { const int64_t millis = rb_bson_to_int64_t(bytes); const VALUE time = rb_time_new(millis / 1000, (millis % 1000) * 1000); return rb_funcall(time, rb_utc_method, 0); }
/* * Private method that converts a psinfo struct into a Ruby struct. */ static VALUE proctable_getprocstruct(struct pst_status *p) { char state[10]; char flag[12]; char ttydev[MAXPATHLEN+1]; VALUE v_tty, v_struct; switch( p->pst_stat ) { case PS_SLEEP: strcpy(state,SLEEP); break; case PS_RUN: strcpy(state,RUN); break; case PS_STOP: strcpy(state,STOP); break; case PS_ZOMBIE: strcpy(state,ZOMBIE); break; case PS_IDLE: strcpy(state,IDLE); break; case PS_OTHER: strcpy(state,OTHER); break; default: strcpy(state,"unknown"); } /* If the major number is -1, there is no associated tty */ if(p->pst_term.psd_major != -1) { devnm( S_IFCHR, (dev_t)((p->pst_term.psd_major << 24) | p->pst_term.psd_minor), ttydev, sizeof(ttydev), 1 ); v_tty = rb_str_new2(ttydev); } else { v_tty = rb_str_new2(""); } v_struct = rb_struct_new(sProcStruct, rb_str_new2(p->pst_ucomm), INT2NUM(p->pst_uid), INT2NUM(p->pst_pid), INT2NUM(p->pst_ppid), INT2NUM(p->pst_dsize), INT2NUM(p->pst_tsize), INT2NUM(p->pst_ssize), INT2NUM(p->pst_nice), v_tty, INT2NUM(p->pst_pgrp), INT2NUM(p->pst_pri), INT2NUM(p->pst_addr), INT2NUM(p->pst_cpu), INT2NUM(p->pst_utime), INT2NUM(p->pst_stime), rb_time_new(p->pst_start,0), INT2NUM(p->pst_flag), rb_str_new2(state), INT2NUM(p->pst_wchan), INT2NUM(p->pst_procnum), rb_str_new2(p->pst_cmd), rb_str_new2(p->pst_cmd), INT2NUM(p->pst_time), INT2NUM(p->pst_cpticks), INT2NUM(p->pst_cptickstotal), INT2NUM(p->pst_fss), rb_float_new(p->pst_pctcpu), INT2NUM(p->pst_rssize), INT2NUM(p->pst_suid), INT2NUM(p->pst_shmsize), INT2NUM(p->pst_mmsize), INT2NUM(p->pst_usize), INT2NUM(p->pst_iosize), INT2NUM(p->pst_vtsize), INT2NUM(p->pst_vdsize), INT2NUM(p->pst_vssize), INT2NUM(p->pst_vshmsize), INT2NUM(p->pst_vmmsize), INT2NUM(p->pst_vusize), INT2NUM(p->pst_viosize), UINT2NUM(p->pst_minorfaults), UINT2NUM(p->pst_majorfaults), UINT2NUM(p->pst_nswap), UINT2NUM(p->pst_nsignals), UINT2NUM(p->pst_msgrcv), UINT2NUM(p->pst_msgsnd), INT2NUM(p->pst_maxrss), INT2NUM(p->pst_sid), INT2NUM(p->pst_schedpolicy), INT2NUM(p->pst_ticksleft), INT2NUM(p->pst_euid), INT2NUM(p->pst_egid), INT2NUM(p->pst_gid), INT2NUM(p->pst_sgid) ); OBJ_FREEZE(v_struct); return v_struct; }
static VALUE time_spec_rb_time_new(VALUE self, VALUE sec, VALUE usec) { return rb_time_new(NUM2LONG(sec), NUM2LONG(usec)); }
/* * call-seq: * ProcTable.ps(pid=nil) * ProcTable.ps(pid=nil){ |ps| ... } * * In block form, yields a ProcTableStruct for each process entry that you * have rights to. This method returns an array of ProcTableStruct's in * non-block form. * * If a +pid+ is provided, then only a single ProcTableStruct is yielded or * returned, or nil if no process information is found for that +pid+. */ static VALUE pt_ps(int argc, VALUE* argv, VALUE klass){ int err; char state[8]; struct kinfo_proc* procs; VALUE v_pid, v_tty_num, v_tty_dev, v_start_time; VALUE v_pstruct = Qnil; VALUE v_array = rb_ary_new(); size_t length, count; size_t i = 0; int g; VALUE v_cmdline, v_exe, v_environ, v_groups; // Passed into sysctl call static const int name_mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; rb_scan_args(argc, argv, "01", &v_pid); // Get size of proc kproc buffer err = sysctl( (int *) name_mib, PROC_MIB_LEN, NULL, &length, NULL, 0); if(err == -1) rb_raise(cProcTableError, "sysctl: %s", strerror(errno)); // Populate the kproc buffer procs = ruby_xmalloc(length); err = sysctl( (int *) name_mib, PROC_MIB_LEN, procs, &length, NULL, 0); if(err == -1) rb_raise(cProcTableError, "sysctl: %s", strerror(errno)); // If we're here, we got our list count = length / sizeof(struct kinfo_proc); for(i = 0; i < count; i++) { v_tty_num = Qnil; v_tty_dev = Qnil; v_start_time = Qnil; // If a PID is provided, skip unless the PID matches if( (!NIL_P(v_pid)) && (procs[i].kp_proc.p_pid != NUM2INT(v_pid)) ) continue; // cmdline will be set only if process exists and belongs to current user or // current user is root v_cmdline = Qnil; v_exe = Qnil; v_environ = Qnil; argv_of_pid(procs[i].kp_proc.p_pid, &v_cmdline, &v_exe, &v_environ); // Get the start time of the process v_start_time = rb_time_new( procs[i].kp_proc.p_un.__p_starttime.tv_sec, procs[i].kp_proc.p_un.__p_starttime.tv_usec ); // Get the state of the process switch(procs[i].kp_proc.p_stat) { case SIDL: strcpy(state, "idle"); break; case SRUN: strcpy(state, "run"); break; case SSLEEP: strcpy(state, "sleep"); break; case SSTOP: strcpy(state, "stop"); break; case SZOMB: strcpy(state, "zombie"); break; default: strcpy(state, "unknown"); break; } // Get ttynum and ttydev. If ttynum is -1, there is no tty. if(procs[i].kp_eproc.e_tdev != -1){ v_tty_num = INT2FIX(procs[i].kp_eproc.e_tdev), v_tty_dev = rb_str_new2(devname(procs[i].kp_eproc.e_tdev, S_IFCHR)); } v_groups = rb_ary_new(); for (g = 0; g < procs[i].kp_eproc.e_ucred.cr_ngroups; ++g) rb_ary_push(v_groups, INT2FIX(procs[i].kp_eproc.e_ucred.cr_groups[g])); v_pstruct = rb_struct_new( sProcStruct, INT2FIX(procs[i].kp_proc.p_pid), INT2FIX(procs[i].kp_eproc.e_ppid), INT2FIX(procs[i].kp_eproc.e_pgid), INT2FIX(procs[i].kp_eproc.e_pcred.p_ruid), INT2FIX(procs[i].kp_eproc.e_pcred.p_rgid), INT2FIX(procs[i].kp_eproc.e_ucred.cr_uid), rb_ary_entry(v_groups, 0), v_groups, INT2FIX(procs[i].kp_eproc.e_pcred.p_svuid), INT2FIX(procs[i].kp_eproc.e_pcred.p_svgid), rb_str_new2(procs[i].kp_proc.p_comm), rb_str_new2(state), rb_float_new(procs[i].kp_proc.p_pctcpu), Qnil, v_tty_num, v_tty_dev, rb_str_new2(procs[i].kp_eproc.e_wmesg), INT2FIX(procs[i].kp_proc.p_rtime.tv_sec), INT2FIX(procs[i].kp_proc.p_priority), INT2FIX(procs[i].kp_proc.p_usrpri), INT2FIX(procs[i].kp_proc.p_nice), v_cmdline, v_exe, v_environ, v_start_time, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_maxrss) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_ixrss) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_idrss) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_isrss) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_minflt) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_majflt) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_nswap) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_inblock) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_oublock) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_msgsnd) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_msgrcv) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_nsignals) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_nvcsw) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_nivcsw) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_utime.tv_sec) : Qnil, (procs[i].kp_proc.p_ru && procs[i].kp_proc.p_stat != 5) ? LONG2NUM(procs[i].kp_proc.p_ru->ru_stime.tv_sec) : Qnil ); OBJ_FREEZE(v_pstruct); // This is read-only data if(rb_block_given_p()) rb_yield(v_pstruct); else rb_ary_push(v_array, v_pstruct); } if(procs) free(procs); if(!rb_block_given_p()){ if(NIL_P(v_pid)) return v_array; else return v_pstruct; } return Qnil; }