Ejemplo n.º 1
0
static VALUE
rb_shadow_sgetspent(VALUE self, VALUE str)
{
  struct spwd *entry;
  VALUE result;

  if( TYPE(str) != T_STRING )
    rb_raise(rb_eException,"argument must be a string.");

  entry = sgetspent(StringValuePtr(str));

  if( entry == NULL )
    return Qnil;

  result = rb_struct_new(rb_sPasswdEntry,
		      rb_tainted_str_new2(entry->sp_namp),
		      rb_tainted_str_new2(entry->sp_pwdp),
		      INT2FIX(entry->sp_lstchg),
		      INT2FIX(entry->sp_min),
		      INT2FIX(entry->sp_max),
		      INT2FIX(entry->sp_warn),
		      INT2FIX(entry->sp_inact),
		      INT2FIX(entry->sp_expire),
		      INT2FIX(entry->sp_flag),
		      0);
  free(entry);
  return result;
};
Ejemplo n.º 2
0
/* creates a Ruby ListenStats Struct based on our internal listen_stats */
static VALUE rb_listen_stats(struct listen_stats *stats)
{
	VALUE active = UINT2NUM(stats->active);
	VALUE queued = UINT2NUM(stats->queued);

	return rb_struct_new(cListenStats, active, queued);
}
Ejemplo n.º 3
0
static VALUE
rb_shadow_fgetspent(VALUE self, VALUE file)
{
  struct spwd *entry;
  VALUE result;

  if( TYPE(file) != T_FILE )
    rb_raise(rb_eTypeError,"argument must be a File.");

  entry = fgetspent(file_ptr(RFILE(file)->fptr));

  if( entry == NULL )
    return Qnil;

  result = rb_struct_new(rb_sPasswdEntry,
		      rb_tainted_str_new2(entry->sp_namp),
		      rb_tainted_str_new2(entry->sp_pwdp),
		      INT2FIX(entry->sp_lstchg),
		      INT2FIX(entry->sp_min),
		      INT2FIX(entry->sp_max),
		      INT2FIX(entry->sp_warn),
		      INT2FIX(entry->sp_inact),
		      INT2FIX(entry->sp_expire),
		      INT2FIX(entry->sp_flag),
		      0);
  return result;
};
Ejemplo n.º 4
0
static VALUE
ptrace_getregs(VALUE self)
{
    struct user_regs_struct urs;
    void *data_ptr = (void *)&urs;
    pid_t pid = get_pid(self);
    long ret;
    VALUE v = Qnil;

    CALL_PTRACE(ret, PT_GETREGS, pid, 0, data_ptr);
    
    v = rb_struct_new(rb_sPTraceRegStruct,
                      ULONG2NUM(urs.rax), ULONG2NUM(urs.rbx),
                      ULONG2NUM(urs.rcx), ULONG2NUM(urs.rdi),
                      ULONG2NUM(urs.rsi), ULONG2NUM(urs.rbp),
                      ULONG2NUM(urs.rsp), ULONG2NUM(urs.r8),
                      ULONG2NUM(urs.r9), ULONG2NUM(urs.r10),
                      ULONG2NUM(urs.r11), ULONG2NUM(urs.r12),
                      ULONG2NUM(urs.r13), ULONG2NUM(urs.r14),
                      ULONG2NUM(urs.r15), ULONG2NUM(urs.rip),
                      ULONG2NUM(urs.cs),
                      ULONG2NUM(urs.fs), ULONG2NUM(urs.gs));

    return v;
}
Ejemplo n.º 5
0
/* grpc_run_batch_stack_build_result fills constructs a ruby BatchResult struct
   after the results have run */
static VALUE grpc_run_batch_stack_build_result(run_batch_stack* st) {
  size_t i = 0;
  VALUE result = rb_struct_new(grpc_rb_sBatchResult, Qnil, Qnil, Qnil, Qnil,
                               Qnil, Qnil, Qnil, Qnil, NULL);
  for (i = 0; i < st->op_num; i++) {
    switch(st->ops[i].op) {
      case GRPC_OP_SEND_INITIAL_METADATA:
        rb_struct_aset(result, sym_send_metadata, Qtrue);
        break;
      case GRPC_OP_SEND_MESSAGE:
        rb_struct_aset(result, sym_send_message, Qtrue);
        grpc_byte_buffer_destroy(st->ops[i].data.send_message);
        break;
      case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
        rb_struct_aset(result, sym_send_close, Qtrue);
        break;
      case GRPC_OP_SEND_STATUS_FROM_SERVER:
        rb_struct_aset(result, sym_send_status, Qtrue);
        break;
      case GRPC_OP_RECV_INITIAL_METADATA:
        rb_struct_aset(result, sym_metadata,
                       grpc_rb_md_ary_to_h(&st->recv_metadata));
      case GRPC_OP_RECV_MESSAGE:
        rb_struct_aset(result, sym_message,
                       grpc_rb_byte_buffer_to_s(st->recv_message));
        break;
      case GRPC_OP_RECV_STATUS_ON_CLIENT:
        rb_struct_aset(
            result,
            sym_status,
            rb_struct_new(grpc_rb_sStatus,
                          UINT2NUM(st->recv_status),
                          (st->recv_status_details == NULL
                           ? Qnil
                           : rb_str_new2(st->recv_status_details)),
                          grpc_rb_md_ary_to_h(&st->recv_trailing_metadata),
                          NULL));
        break;
      case GRPC_OP_RECV_CLOSE_ON_SERVER:
        rb_struct_aset(result, sym_send_close, Qtrue);
        break;
      default:
        break;
    }
  }
  return result;
}
Ejemplo n.º 6
0
// get function parameter info
static VALUE ta_func_param_info(int param_type, VALUE self, VALUE name, VALUE index)
{
  TA_RetCode ret_code;
  const TA_FuncHandle *handle;

  ret_code = TA_GetFuncHandle( StringValuePtr(name), &handle );
  if ( ret_code == TA_SUCCESS )
  {
    switch (param_type)
    {
      case TA_INPUT_PARAM:
      {
        const TA_InputParameterInfo *param_info;

        ret_code = TA_GetInputParameterInfo( handle, FIX2INT(index), &param_info );
        if (ret_code != TA_SUCCESS)
          rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetInputParameterInfo");
        return rb_struct_new(rb_sInParamInfo, INT2FIX(param_info->type), rb_str_new2(param_info->paramName), INT2FIX(param_info->flags),NULL);
      }
      break;
      case TA_OPTION_INPUT_PARAM:
      {
        const TA_OptInputParameterInfo *param_info;

        ret_code = TA_GetOptInputParameterInfo( handle, FIX2INT(index), &param_info );
        if (ret_code != TA_SUCCESS)
          rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetOptInputParameterInfo");
        // FIXME: helpFile = Qnil
        // FIXME: dataSet = Qnil
        return rb_struct_new(rb_sOptInParamInfo, INT2FIX(param_info->type), rb_str_new2(param_info->paramName), INT2FIX(param_info->flags), rb_str_new2(param_info->displayName), Qnil, rb_float_new(param_info->defaultValue), rb_str_new2(param_info->hint), Qnil, NULL);
      }
      break;
      case TA_OUTPUT_PARAM:
      {
        const TA_OutputParameterInfo *param_info;

        ret_code = TA_GetOutputParameterInfo( handle, FIX2INT(index), &param_info );
        if (ret_code != TA_SUCCESS)
          rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetOutputParameterInfo");
        return rb_struct_new(rb_sOutParamInfo, INT2FIX(param_info->type), rb_str_new2(param_info->paramName), INT2FIX(param_info->flags),NULL);
      }
      break;
    } // switch
  }
  rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetFuncHandle");
}
Ejemplo n.º 7
0
VALUE
GimpParasite2rb (GimpParasite leech)
{
  volatile VALUE name = rb_str_new2(leech.name);
  volatile VALUE flags = UINT2NUM(leech.flags);
  volatile VALUE data = rb_str_new(leech.data, leech.size);

  return rb_struct_new(sGimpParasite, name, flags, data, NULL);
}
Ejemplo n.º 8
0
VALUE
GimpParamDef2rb (GimpParamDef paramdef)
{
  volatile VALUE type = INT2NUM(paramdef.type);
  volatile VALUE name = rb_str_new2(paramdef.name);
  volatile VALUE desc = rb_str_new2(paramdef.description);

  return rb_struct_new(sGimpParamDef, type, name, desc, NULL);
}
Ejemplo n.º 9
0
/* call-seq:
   cq = CompletionQueue.new
   tag = Object.new
   timeout = 10
   server.request_call(cqueue, tag, timeout)

   Requests notification of a new call on a server. */
static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue,
                                         VALUE tag_new, VALUE timeout) {
  grpc_rb_server *s = NULL;
  grpc_call *call = NULL;
  grpc_event *ev = NULL;
  grpc_call_error err;
  request_call_stack st;
  VALUE result;
  Data_Get_Struct(self, grpc_rb_server, s);
  if (s->wrapped == NULL) {
    rb_raise(rb_eRuntimeError, "closed!");
    return Qnil;
  } else {
    grpc_request_call_stack_init(&st);
    /* call grpc_server_request_call, then wait for it to complete using
     * pluck_event */
    err = grpc_server_request_call(
        s->wrapped, &call, &st.details, &st.md_ary,
        grpc_rb_get_wrapped_completion_queue(cqueue),
        ROBJECT(tag_new));
    if (err != GRPC_CALL_OK) {
      grpc_request_call_stack_cleanup(&st);
      rb_raise(grpc_rb_eCallError,
              "grpc_server_request_call failed: %s (code=%d)",
               grpc_call_error_detail_of(err), err);
      return Qnil;
    }
    ev = grpc_rb_completion_queue_pluck_event(cqueue, tag_new, timeout);
    if (ev == NULL) {
      grpc_request_call_stack_cleanup(&st);
      return Qnil;
    }
    if (ev->data.op_complete != GRPC_OP_OK) {
      grpc_request_call_stack_cleanup(&st);
      grpc_event_finish(ev);
      rb_raise(grpc_rb_eCallError, "request_call completion failed: (code=%d)",
               ev->data.op_complete);
      return Qnil;
    }

    /* build the NewServerRpc struct result */
    result = rb_struct_new(
        grpc_rb_sNewServerRpc,
        rb_str_new2(st.details.method),
        rb_str_new2(st.details.host),
        rb_funcall(rb_cTime, id_at, 2, INT2NUM(st.details.deadline.tv_sec),
                   INT2NUM(st.details.deadline.tv_nsec)),
        grpc_rb_md_ary_to_h(&st.md_ary),
        grpc_rb_wrap_call(call),
        NULL);
    grpc_event_finish(ev);
    grpc_request_call_stack_cleanup(&st);
    return result;
  }
  return Qnil;
}
Ejemplo n.º 10
0
/* call-seq:
   cq = CompletionQueue.new
   tag = Object.new
   timeout = 10
   server.request_call(cqueue, tag, timeout)

   Requests notification of a new call on a server. */
static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue,
        VALUE tag_new, VALUE timeout) {
    grpc_rb_server *s = NULL;
    grpc_call *call = NULL;
    grpc_event ev;
    grpc_call_error err;
    request_call_stack st;
    VALUE result;
    gpr_timespec deadline;
    TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
    if (s->wrapped == NULL) {
        rb_raise(rb_eRuntimeError, "destroyed!");
        return Qnil;
    } else {
        grpc_request_call_stack_init(&st);
        /* call grpc_server_request_call, then wait for it to complete using
         * pluck_event */
        err = grpc_server_request_call(
                  s->wrapped, &call, &st.details, &st.md_ary,
                  grpc_rb_get_wrapped_completion_queue(cqueue),
                  grpc_rb_get_wrapped_completion_queue(cqueue),
                  ROBJECT(tag_new));
        if (err != GRPC_CALL_OK) {
            grpc_request_call_stack_cleanup(&st);
            rb_raise(grpc_rb_eCallError,
                     "grpc_server_request_call failed: %s (code=%d)",
                     grpc_call_error_detail_of(err), err);
            return Qnil;
        }

        ev = grpc_rb_completion_queue_pluck_event(cqueue, tag_new, timeout);
        if (ev.type == GRPC_QUEUE_TIMEOUT) {
            grpc_request_call_stack_cleanup(&st);
            return Qnil;
        }
        if (!ev.success) {
            grpc_request_call_stack_cleanup(&st);
            rb_raise(grpc_rb_eCallError, "request_call completion failed");
            return Qnil;
        }

        /* build the NewServerRpc struct result */
        deadline = gpr_convert_clock_type(st.details.deadline, GPR_CLOCK_REALTIME);
        result = rb_struct_new(
                     grpc_rb_sNewServerRpc, rb_str_new2(st.details.method),
                     rb_str_new2(st.details.host),
                     rb_funcall(rb_cTime, id_at, 2, INT2NUM(deadline.tv_sec),
                                INT2NUM(deadline.tv_nsec)),
                     grpc_rb_md_ary_to_h(&st.md_ary), grpc_rb_wrap_call(call), NULL);
        grpc_request_call_stack_cleanup(&st);
        return result;
    }
    return Qnil;
}
Ejemplo n.º 11
0
static VALUE
make_pair(VALUE pair_def, int before_open_len, VALUE around_open_tokens, VALUE outer)
{
  VALUE pair = rb_struct_new(Pair, 
      pair_def,
      INT2FIX(before_open_len),
      around_open_tokens,
      Qnil,
      Qnil,
      outer);
  return pair;
}
Ejemplo n.º 12
0
/* call-seq:
   server.request_call

   Requests notification of a new call on a server. */
static VALUE grpc_rb_server_request_call(VALUE self) {
  grpc_rb_server *s = NULL;
  grpc_call *call = NULL;
  grpc_event ev;
  grpc_call_error err;
  request_call_stack st;
  VALUE result;
  void *tag = (void*)&st;
  grpc_completion_queue *call_queue = grpc_completion_queue_create(NULL);
  gpr_timespec deadline;

  TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
  if (s->wrapped == NULL) {
    rb_raise(rb_eRuntimeError, "destroyed!");
    return Qnil;
  }
  grpc_request_call_stack_init(&st);
  /* call grpc_server_request_call, then wait for it to complete using
   * pluck_event */
  err = grpc_server_request_call(
      s->wrapped, &call, &st.details, &st.md_ary,
      call_queue, s->queue, tag);
  if (err != GRPC_CALL_OK) {
    grpc_request_call_stack_cleanup(&st);
    rb_raise(grpc_rb_eCallError,
             "grpc_server_request_call failed: %s (code=%d)",
             grpc_call_error_detail_of(err), err);
    return Qnil;
  }

  ev = rb_completion_queue_pluck(s->queue, tag,
                                 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  if (!ev.success) {
    grpc_request_call_stack_cleanup(&st);
    rb_raise(grpc_rb_eCallError, "request_call completion failed");
    return Qnil;
  }



  /* build the NewServerRpc struct result */
  deadline = gpr_convert_clock_type(st.details.deadline, GPR_CLOCK_REALTIME);
  result = rb_struct_new(
      grpc_rb_sNewServerRpc, grpc_rb_slice_to_ruby_string(st.details.method),
      grpc_rb_slice_to_ruby_string(st.details.host),
      rb_funcall(rb_cTime, id_at, 2, INT2NUM(deadline.tv_sec),
                 INT2NUM(deadline.tv_nsec / 1000)),
      grpc_rb_md_ary_to_h(&st.md_ary), grpc_rb_wrap_call(call, call_queue),
      NULL);
  grpc_request_call_stack_cleanup(&st);
  return result;
}
Ejemplo n.º 13
0
static VALUE
process_times(VALUE self) {
	struct rusage usage;
	unsigned long long utime, stime;
	
	if (getrusage(RUSAGE_SELF, &usage) == -1) {
		rb_sys_fail("getrusage()");
	}
	
	utime = (unsigned long long) usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec;
	stime = (unsigned long long) usage.ru_stime.tv_sec * 1000000 + usage.ru_stime.tv_usec;
	return rb_struct_new(S_ProcessTimes, rb_ull2inum(utime), rb_ull2inum(stime));
}
Ejemplo n.º 14
0
/**
 * Decodes protobuf message.
 *
 * buffer - pointer to the buffer with message data
 * buffer_size - size of the buffee
 *
 * Returns ruby array with decoded fields.
 *
 */
static VALUE decode_protobuf( uint8_t *buffer, long buffer_size )
{
    // Will return array of fields
    VALUE message = rb_ary_new();

    while ( buffer_size > 0 )
    {
        // New field
        VALUE field = rb_struct_new( rb_sProtobufCoderField, Qnil, Qnil, Qnil );

        // Read field tag
        uint64_t tag = read_varint( &buffer, &buffer_size );

        uint64_t field_number = tag >> 3;
        uint64_t wire_type    = tag & 0x07;

        rb_struct_aset( field, ID2SYM( rb_intern( "number" )), INT2FIX( field_number ));
        rb_struct_aset( field, ID2SYM( rb_intern( "type" )), INT2FIX( wire_type ));

        switch ( wire_type )
        {
            case WT_VARINT:
                // Read VARINT
                rb_struct_aset( field, ID2SYM( rb_intern( "value" )), INT2NUM( read_varint( &buffer, &buffer_size )));
                break;

            case WT_STRING:
                // First - read one VARINT with string length, then read "length" bytes
                rb_struct_aset( field, ID2SYM( rb_intern( "value" )), read_bytes( &buffer, &buffer_size, read_varint( &buffer, &buffer_size )));
                break;

            case WT_64BIT:
                // Read 8 bytes
                rb_struct_aset( field, ID2SYM( rb_intern( "value" )), read_bytes( &buffer, &buffer_size, 8 ));
                break;

            case WT_32BIT:
                // Read 4 bytes
                rb_struct_aset( field, ID2SYM( rb_intern( "value" )), read_bytes( &buffer, &buffer_size, 4 ));
                break;

            default:
                rb_raise( rb_eProtobufCoderDecodingError, "invalid wire type: %lld", wire_type );
        }

        rb_ary_push( message, field );
    }

    return message;
}
Ejemplo n.º 15
0
static VALUE
console_info(VALUE io)
{
    int fd = NUM2INT(rb_funcallv(io, rb_intern("fileno"), 0, 0));
    HANDLE h = (HANDLE)rb_w32_get_osfhandle(fd);
    CONSOLE_SCREEN_BUFFER_INFO csbi;

    if (h == (HANDLE)-1) rb_raise(rb_eIOError, "invalid io");
    if (!GetConsoleScreenBufferInfo(h, &csbi))
	rb_syserr_fail(rb_w32_map_errno(GetLastError()), "not console");
    return rb_struct_new(rb_cConsoleScreenBufferInfo,
			 INT2FIX(csbi.dwSize.X), INT2FIX(csbi.dwSize.Y),
			 INT2FIX(csbi.dwCursorPosition.X), INT2FIX(csbi.dwCursorPosition.Y),
			 INT2FIX(csbi.wAttributes));
}
Ejemplo n.º 16
0
static VALUE convert_pw_struct( struct spwd *entry )
{
    return rb_struct_new(rb_sPasswdEntry,
                         rb_tainted_str_new2(entry->sp_namp),
                         rb_tainted_str_new2(entry->sp_pwdp),
                         INT2FIX(entry->sp_lstchg),
                         INT2FIX(entry->sp_min),
                         INT2FIX(entry->sp_max),
                         INT2FIX(entry->sp_warn),
                         INT2FIX(entry->sp_inact),
                         Qnil, /* used by BSD, pw_change, date when the password expires, in days since Jan 1, 1970 */
                         INT2FIX(entry->sp_expire),
                         INT2FIX(entry->sp_flag),
                         NULL);
};
Ejemplo n.º 17
0
static VALUE rb_receive (VALUE self_, VALUE block_)
{
    //  Get the context.
    context_t* context;
    Data_Get_Struct (self_, context_t, context);
	
    //  Forward the call to native 0MQ library.
    zmq::message_t msg;
    int qid = context->api_thread->receive (&msg, 
        NUM2INT(block_) ? true : false);
    
    VALUE rb_msg = rb_str_new ((char *) msg.data (), msg.size ());
    VALUE rb_type = INT2NUM (msg.type ());
    VALUE rb_qid = INT2NUM (qid);
    
    return rb_struct_new (rb_data, rb_msg, rb_type, rb_qid, NULL);
}
Ejemplo n.º 18
0
static VALUE
setup_group(struct group *grp)
{
    VALUE mem;
    char **tbl;

    mem = rb_ary_new();
    tbl = grp->gr_mem;
    while (*tbl) {
	rb_ary_push(mem, safe_setup_str(*tbl));
	tbl++;
    }
    return rb_struct_new(sGroup,
			 safe_setup_str(grp->gr_name),
#ifdef HAVE_ST_GR_PASSWD
			 safe_setup_str(grp->gr_passwd),
#endif
			 GIDT2NUM(grp->gr_gid),
			 mem);
}
Ejemplo n.º 19
0
/*
 * Returns a struct of type UnameStruct that contains sysname, nodename,
 * machine, version, and release.  On Solaris, it will also include 
 * architecture and platform.  On HP-UX, it will also include id_number.
 */
static VALUE uname_uname_all()
{
   struct utsname u;
   uname(&u);

/* Extra brackets are for C89 compliance */
{
#ifdef HAVE_SYS_SYSTEMINFO_H
   char platform[BUFSIZE];
   char arch[BUFSIZE];
   sysinfo(SI_ARCHITECTURE, arch, BUFSIZE);
   sysinfo(SI_PLATFORM, platform, BUFSIZE);
#endif

#ifdef HAVE_SYSCTL
   char model[BUFSIZ];
   get_model(model, sizeof(model));
#endif

   return rb_struct_new(sUname,
      rb_str_new2(u.sysname),
      rb_str_new2(u.nodename),
      rb_str_new2(u.machine),
      rb_str_new2(u.version),
      rb_str_new2(u.release)
#ifdef HAVE_SYS_SYSTEMINFO_H
      ,rb_str_new2(arch),
      rb_str_new2(platform)
#endif

#ifdef HAVE_SYSCTL
      ,rb_str_new2(model)
#endif

#if defined(__hpux)
      ,rb_str_new2(u.__idnumber)
#endif
   );
}
}
Ejemplo n.º 20
0
/*
 * call-seq:
 *    Proto.getprotoent
 *    Proto.getprotoent{ |struct| ... }
 *
 * In block form, yields each entry from /etc/protocols as a struct of type
 * Proto::ProtoStruct.  In non-block form, returns an array of
 * Proto::ProtoStruct objects.
	
 * The fields are 'name' (a String), 'aliases' (an Array of String's,
 * though often only one element), and 'proto' (a Fixnum).
 *
 * Example:
 *
 *   Net::Proto.getprotoent.each{ |prot|
 *      p prot.name
 *      p prot.aliases
 *      p prot.proto
 *   }
*/
static VALUE np_getprotoent(){
   struct protoent p;
   char buffer[BUF_SIZE];
   VALUE v_alias_array = Qnil;
   VALUE v_array = Qnil;
   VALUE v_struct = Qnil;
   
   if(!rb_block_given_p())
   	v_array = rb_ary_new();

   setprotoent(0);

   while(getprotoent_r(&p, buffer, BUF_SIZE)){
      v_alias_array = rb_ary_new();
      
      while(*p.p_aliases){
         rb_ary_push(v_alias_array ,rb_str_new2(*p.p_aliases));
         (void)p.p_aliases++;
      }
      
      v_struct = rb_struct_new(sProto,
      	rb_str_new2(p.p_name),
      	v_alias_array,
      	INT2FIX(p.p_proto)
      );

      OBJ_FREEZE(v_struct); /* This is read-only data */

      if(rb_block_given_p())
         rb_yield(v_struct);
      else
         rb_ary_push(v_array, v_struct);
   }

   endprotoent();

   return v_array; /* nil unless a block is given */
}
Ejemplo n.º 21
0
static VALUE
setup_passwd(struct passwd *pwd)
{
    if (pwd == 0) rb_sys_fail("/etc/passwd");
    return rb_struct_new(sPasswd,
			 safe_setup_str(pwd->pw_name),
#ifdef HAVE_ST_PW_PASSWD
			 safe_setup_str(pwd->pw_passwd),
#endif
			 UIDT2NUM(pwd->pw_uid),
			 GIDT2NUM(pwd->pw_gid),
#ifdef HAVE_ST_PW_GECOS
			 safe_setup_str(pwd->pw_gecos),
#endif
			 safe_setup_str(pwd->pw_dir),
			 safe_setup_str(pwd->pw_shell),
#ifdef HAVE_ST_PW_CHANGE
			 INT2NUM(pwd->pw_change),
#endif
#ifdef HAVE_ST_PW_QUOTA
			 INT2NUM(pwd->pw_quota),
#endif
#ifdef HAVE_ST_PW_AGE
			 PW_AGE2VAL(pwd->pw_age),
#endif
#ifdef HAVE_ST_PW_CLASS
			 safe_setup_str(pwd->pw_class),
#endif
#ifdef HAVE_ST_PW_COMMENT
			 safe_setup_str(pwd->pw_comment),
#endif
#ifdef HAVE_ST_PW_EXPIRE
			 INT2NUM(pwd->pw_expire),
#endif
			 0		/*dummy*/
	);
}
Ejemplo n.º 22
0
static void*
transaction_callback(const void* hd, const rpmCallbackType type,
					 const rpmCallbackSize_t amount, const rpmCallbackSize_t total,
					 fnpyKey key, rpmCallbackData data)
{
	VALUE trans = (VALUE)data;
	FD_t fdt;
	const Header hdr = (Header)hd;
	VALUE sig;
	VALUE rv;

	sig = rb_struct_new(rpm_sCallbackData, INT2NUM(type), key ? (VALUE)key:Qnil,
						rpm_package_new_from_header(hdr),
						UINT2NUM(amount), UINT2NUM(total));

	rv = rb_yield(sig);

	switch (type) {
	case RPMCALLBACK_INST_OPEN_FILE:
		if (TYPE(rv) != T_FILE) {
			rb_raise(rb_eTypeError, "illegal return value type");
		}
		rb_ivar_set(trans, id_file, rv);
		fdt = fdDup(NUM2INT(rb_Integer(rv)));
		rb_ivar_set(trans, id_fdt, INT2NUM((long)fdt));
		return fdt;

	case RPMCALLBACK_INST_CLOSE_FILE:
		Fclose((FD_t)NUM2LONG(rb_ivar_get(trans, id_fdt)));
		rb_ivar_set(trans, id_file, Qnil);
		rb_ivar_set(trans, id_fdt, Qnil);
	default:
		break;
	}

	return NULL;
}
Ejemplo n.º 23
0
static VALUE
rb_shadow_getspent(VALUE self)
{
  struct spwd *entry;
  VALUE result;

  entry = getspent();

  if( entry == NULL )
    return Qnil;

  result = rb_struct_new(rb_sPasswdEntry,
		      rb_tainted_str_new2(entry->sp_namp),
		      rb_tainted_str_new2(entry->sp_pwdp),
		      INT2FIX(entry->sp_lstchg),
		      INT2FIX(entry->sp_min),
		      INT2FIX(entry->sp_max),
		      INT2FIX(entry->sp_warn),
		      INT2FIX(entry->sp_inact),
		      INT2FIX(entry->sp_expire),
		      INT2FIX(entry->sp_flag),
		      0);
  return result;
};
Ejemplo n.º 24
0
static
VALUE
rb_mouse_wrap_point(const CGPoint point)
{
    return rb_struct_new(rb_cCGPoint, DBL2NUM(point.x), DBL2NUM(point.y));
}
Ejemplo n.º 25
0
/*
 * 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){
   kvm_t *kd;
   char errbuf[_POSIX2_LINE_MAX];
   char cmdline[_POSIX_ARG_MAX+1];
   char state[8];
   char** args = malloc(sizeof(char*));
   struct kinfo_proc* procs;
   int count;                     /* Holds total number of processes */
   int i = 0;
   VALUE v_pid, v_tty_num, v_tty_dev, v_start_time;
   VALUE v_pstruct = Qnil;
   VALUE v_array = Qnil;

   rb_scan_args(argc, argv, "01", &v_pid);

   if(!rb_block_given_p())
      v_array = rb_ary_new();

   // Open the kvm interface, get a descriptor
   if ((kd = kvm_open(NULL, NULL, NULL, 0, errbuf)) == NULL)
      rb_raise(cProcTableError, errbuf);

   // Get the list of processes
   if ((procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count)) == NULL) {
      strcpy(errbuf,kvm_geterr(kd));
      kvm_close(kd);
      rb_raise(cProcTableError, errbuf);
   }

   for(i=0; i<count; i++){
      // Reset some variables
      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)){
#ifdef HAVE_ST_KP_PROC
         if(procs[i].kp_proc.p_pid != NUM2INT(v_pid))
            continue;
#else
         if(procs[i].ki_pid != NUM2INT(v_pid))
            continue;
#endif
      }

      // Get the command line arguments for the process
      cmdline[0] = '\0';
      args = kvm_getargv(kd, (const struct kinfo_proc *)&procs[i], 0);
      if(args){
         int j = 0;
         while (args[j] && strlen(cmdline) <= _POSIX_ARG_MAX) {
            strcat(cmdline, args[j]);
            strcat(cmdline, " ");
            j++;
          }
      }

      // Get the start time of the process
      v_start_time = rb_time_new(
#ifdef HAVE_ST_E_STATS
         procs[i].kp_eproc.e_stats.p_start.tv_sec,
         procs[i].kp_eproc.e_stats.p_start.tv_usec
#else
         0,0
#endif
      );

      // Get the state of the process
#ifdef HAVE_ST_KP_PROC
      switch(procs[i].kp_proc.p_stat)
#else
      switch(procs[i].ki_stat)
#endif
      {
         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
#ifdef HAVE_ST_KP_EPROC
      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));
#elif HAVE_ST_U_KPROC
      v_tty_num = INT2FIX(procs[i].u_kproc.ki_tdev),
      v_tty_dev = rb_str_new2(devname(procs[i].u_kproc.ki_tdev, S_IFCHR));
#else
      v_tty_num = INT2FIX(procs[i].ki_tdev),
      v_tty_dev = rb_str_new2(devname(procs[i].ki_tdev, S_IFCHR));
#endif

#ifdef HAVE_ST_KP_PROC
      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),
#ifdef HAVE_ST_P_ONCPU
         INT2FIX(procs[i].kp_proc.p_oncpu),
#else
         Qnil,
#endif
         v_tty_num,
         v_tty_dev,
         rb_str_new2(procs[i].kp_eproc.e_wmesg),
#ifdef HAVE_ST_P_RUNTIME
         INT2FIX(procs[i].kp_proc.p_runtime/1000000),
#else
         Qnil,
#endif
         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,
#ifdef HAVE_ST_E_STATS
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_maxrss),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_ixrss),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_idrss),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_isrss),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_minflt),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_majflt),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_nswap),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_inblock),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_oublock),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_msgsnd),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_msgrcv),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_nsignals),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_nvcsw),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_nivcsw),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_utime.tv_sec),
         LONG2NUM(procs[i].kp_eproc.e_stats.p_ru.ru_stime.tv_sec)
#else
         Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil,
         Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil
#endif
      );
#else
      v_pstruct = rb_struct_new(
         sProcStruct,
         INT2FIX(procs[i].ki_pid),
         INT2FIX(procs[i].ki_ppid),
         INT2FIX(procs[i].ki_pgid),
         INT2FIX(procs[i].ki_ruid),
         INT2FIX(procs[i].ki_rgid),
         rb_str_new2(procs[i].ki_ocomm),
         rb_str_new2(state),
         rb_float_new(procs[i].ki_pctcpu),
         INT2FIX(procs[i].ki_oncpu),
         v_tty_num,
         v_tty_dev,
         rb_str_new2(procs[i].ki_wmesg),
         INT2FIX(procs[i].ki_runtime/1000000),
         INT2FIX(procs[i].ki_pri.pri_level),
         INT2FIX(procs[i].ki_pri.pri_user),
         INT2FIX(procs[i].ki_nice),
         rb_str_new2(cmdline),
         v_start_time,
         Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil,
         Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil
      );
#endif

      OBJ_FREEZE(v_pstruct); /* Read-only data */

      if(rb_block_given_p())
         rb_yield(v_pstruct);
      else
         rb_ary_push(v_array, v_pstruct);
   }

   free(args);

   if(kd)
      kvm_close(kd);

   if(!NIL_P(v_pid))
      return v_pstruct;

   return v_array; // Nil if block given
}
Ejemplo n.º 26
0
VALUE
GimpParam2rb (const GimpParam *params,
              int              index)
{
  const GimpParam *param = params + index;
  GimpPDBArgType type = param->type;
  volatile VALUE data;

  switch (type)
    {
    case GIMP_PDB_INT32:
      data = INT2NUM(param->data.d_int32);
      break;

    case GIMP_PDB_INT16:
      data = INT2NUM(param->data.d_int16);
      break;

    case GIMP_PDB_INT8:
      data = INT2NUM(param->data.d_int8);
      break;

    case GIMP_PDB_FLOAT:
      data = rb_float_new(param->data.d_float);
      break;

    case GIMP_PDB_STRING:
      data = rb_str_new2(param->data.d_string);
      break;

    case GIMP_PDB_INT32ARRAY:
    case GIMP_PDB_INT16ARRAY:
    case GIMP_PDB_FLOATARRAY:
    case GIMP_PDB_STRINGARRAY:
      data = gimp_array2rb(params, index);
      break;

    case GIMP_PDB_INT8ARRAY:
      data = int8array2str(params, index);
      break;

    case GIMP_PDB_COLOR:
      data = GimpRGB2rb(&param->data.d_color);
      break;

/*    case GIMP_PDB_REGION:
      data = GimpParamRegion2rb(&param->data.d_region);
      break;*/

    case GIMP_PDB_DISPLAY:
      data = INT2NUM(param->data.d_display);
      break;

    case GIMP_PDB_IMAGE:
      data = INT2NUM(param->data.d_image);
      break;

    case GIMP_PDB_LAYER:
      data = INT2NUM(param->data.d_layer);
      break;

/*    case GIMP_PDB_LAYER_MASK:
      data = INT2NUM(param.data.d_layer_mask);
      break;*/

    case GIMP_PDB_CHANNEL:
      data = INT2NUM(param->data.d_channel);
      break;

    case GIMP_PDB_DRAWABLE:
      data = INT2NUM(param->data.d_drawable);
      break;

    case GIMP_PDB_SELECTION:
      data = INT2NUM(param->data.d_selection);
      break;

    case GIMP_PDB_BOUNDARY:
      data = INT2NUM(param->data.d_boundary);
      break;

    case GIMP_PDB_VECTORS:
      data = INT2NUM(param->data.d_vectors);
      break;

/*    case GIMP_PDB_UNIT:
      data = INT2NUM(param.data.d_unit);
      break;*/

    case GIMP_PDB_PARASITE:
      data = GimpParasite2rb(param->data.d_parasite);
      break;

/*   case GIMP_PDB_TATTOO:
     data = INT2NUM(param.data.d_tattoo);
     break;*/

    case GIMP_PDB_STATUS:
      data = INT2NUM(param->data.d_status);
      break;

    default:
      rb_bug("Parameter type %d does not exist.\n", param->type);
    }

  return rb_struct_new(sGimpParam, INT2NUM(type), data);
}
Ejemplo n.º 27
0
/*
 * 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;
}
Ejemplo n.º 28
0
static VALUE struct_spec_rb_struct_new(VALUE self, VALUE klass,
                                       VALUE a, VALUE b, VALUE c)
{

  return rb_struct_new(klass, a, b, c);
}
Ejemplo n.º 29
0
/*
 * 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;
}
Ejemplo n.º 30
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;
}