diff --git a/ext/hbsdctl.rb/feature.c b/ext/hbsdctl.rb/feature.c new file mode 100644 index 0000000..9377df4 --- /dev/null +++ b/ext/hbsdctl.rb/feature.c @@ -0,0 +1,44 @@ +#include +#include +#include "include/feature.h" +static VALUE get_rb_eError(void); + +/** + * BSD::Control::Feature#set! + **/ +VALUE +feature_set(VALUE self, VALUE path, VALUE state) +{ + int r; + char *cpath; + VALUE name; + + if (getuid() != 0) { + rb_raise(get_rb_eError(), "This operation requires root privileges."); + } + Check_Type(path, T_STRING); + Check_Type(state, T_FIXNUM); + cpath = RSTRING_PTR(path); + name = rb_funcall(self, rb_intern("name"), 0); + Check_Type(name, T_STRING); + r = hbsdcontrol_set_feature_state( + cpath, + RSTRING_PTR(name), + NUM2INT(state) + ); + if (r == 0) { + return (Qtrue); + } else { + rb_raise(get_rb_eError(), "hbsdcontrol_set_feature_state failed"); + } +} + + +static VALUE +get_rb_eError(void) +{ + VALUE rb_mBSD = rb_const_get(rb_cObject, rb_intern("BSD")), + rb_mControl = rb_const_get(rb_mBSD, rb_intern("Control")), + rb_eError = rb_const_get(rb_mControl, rb_intern("Error")); + return (rb_eError); +} diff --git a/ext/hbsdctl.rb/ffi.c b/ext/hbsdctl.rb/ffi.c new file mode 100644 index 0000000..2348050 --- /dev/null +++ b/ext/hbsdctl.rb/ffi.c @@ -0,0 +1,68 @@ +#include +#include +#include "include/ffi.h" + +/** + * BSD::Control::FFI.available_features + **/ +VALUE +ffi_available_features(VALUE self) +{ + const struct pax_feature_entry *entry = &pax_features[0]; + VALUE rb_mBSD = rb_const_get(rb_cObject, rb_intern("BSD")), + rb_mControl = rb_const_get(rb_mBSD, rb_intern("Control")), + rb_cFeature = rb_const_get(rb_mControl, rb_intern("Feature")), + features = rb_ary_new(), + feature = 0; + + while (entry->feature != NULL) + { + feature = rb_funcall( + rb_cFeature, + rb_intern("new"), + 3, + rb_str_new2(entry->feature), + rb_str_new2(entry->extattr[1]), + rb_str_new2(entry->extattr[0]) + ); + rb_ary_push(features, feature); + entry++; + } + return (features); +} + + +/** + * BSD::Control::FFI.reset! + **/ +VALUE +ffi_reset(VALUE self, VALUE rb_feature, VALUE rb_path) +{ + VALUE rb_enable_flag, rb_disable_flag; + char *enable_flag, *disable_flag, *path; + int r; + + rb_enable_flag = rb_funcall(rb_feature, rb_intern("enable"), 0); + rb_disable_flag = rb_funcall(rb_feature, rb_intern("disable"), 0); + Check_Type(rb_path, T_STRING); + Check_Type(rb_enable_flag, T_STRING); + Check_Type(rb_disable_flag, T_STRING); + path = RSTRING_PTR(rb_path); + enable_flag = RSTRING_PTR(rb_enable_flag); + disable_flag = RSTRING_PTR(rb_disable_flag); + r = hbsdcontrol_extattr_rm_attr(path, disable_flag); + r &= hbsdcontrol_extattr_rm_attr(path, enable_flag); + return (r == 0 ? Qtrue : Qfalse); +} + + +/** + * BSD::Control::FFI.library_version + **/ +VALUE +ffi_library_version(VALUE self) +{ + const char *ver; + ver = hbsdcontrol_get_version(); + return (rb_str_new2(ver)); +} diff --git a/ext/hbsdctl.rb/hbsdctl.c b/ext/hbsdctl.rb/hbsdctl.c index 337a802..ac29d96 100644 --- a/ext/hbsdctl.rb/hbsdctl.c +++ b/ext/hbsdctl.rb/hbsdctl.c @@ -1,101 +1,6 @@ #include -#include -#include - -static VALUE -get_rb_eError(void) -{ - VALUE rb_mBSD = rb_const_get(rb_cObject, rb_intern("BSD")), - rb_mControl = rb_const_get(rb_mBSD, rb_intern("Control")), - rb_eError = rb_const_get(rb_mControl, rb_intern("Error")); - return (rb_eError); -} - - -static VALUE -ffi_library_version(VALUE self) -{ - const char *ver; - ver = hbsdcontrol_get_version(); - return (rb_str_new2(ver)); -} - - -static VALUE -ffi_available_features(VALUE self) -{ - const struct pax_feature_entry *entry = &pax_features[0]; - VALUE rb_mBSD = rb_const_get(rb_cObject, rb_intern("BSD")), - rb_mControl = rb_const_get(rb_mBSD, rb_intern("Control")), - rb_cFeature = rb_const_get(rb_mControl, rb_intern("Feature")), - features = rb_ary_new(), - feature = 0; - - while (entry->feature != NULL) - { - feature = rb_funcall( - rb_cFeature, - rb_intern("new"), - 3, - rb_str_new2(entry->feature), - rb_str_new2(entry->extattr[1]), - rb_str_new2(entry->extattr[0]) - ); - rb_ary_push(features, feature); - entry++; - } - return (features); -} - - -static VALUE -ffi_reset(VALUE self, VALUE rb_feature, VALUE rb_path) -{ - VALUE rb_enable_flag, rb_disable_flag; - char *enable_flag, *disable_flag, *path; - int r; - - rb_enable_flag = rb_funcall(rb_feature, rb_intern("enable"), 0); - rb_disable_flag = rb_funcall(rb_feature, rb_intern("disable"), 0); - Check_Type(rb_path, T_STRING); - Check_Type(rb_enable_flag, T_STRING); - Check_Type(rb_disable_flag, T_STRING); - path = RSTRING_PTR(rb_path); - enable_flag = RSTRING_PTR(rb_enable_flag); - disable_flag = RSTRING_PTR(rb_disable_flag); - r = hbsdcontrol_extattr_rm_attr(path, disable_flag); - r &= hbsdcontrol_extattr_rm_attr(path, enable_flag); - return (r == 0 ? Qtrue : Qfalse); -} - - -static VALUE -feature_set(VALUE self, VALUE path, VALUE state) -{ - int r; - char *cpath; - VALUE name; - - if (getuid() != 0) { - rb_raise(get_rb_eError(), "This operation requires root privileges."); - } - Check_Type(path, T_STRING); - Check_Type(state, T_FIXNUM); - cpath = RSTRING_PTR(path); - name = rb_funcall(self, rb_intern("name"), 0); - Check_Type(name, T_STRING); - r = hbsdcontrol_set_feature_state( - cpath, - RSTRING_PTR(name), - NUM2INT(state) - ); - if (r == 0) { - return (Qtrue); - } else { - rb_raise(get_rb_eError(), "hbsdcontrol_set_feature_state failed"); - } -} - +#include "include/ffi.h" +#include "include/feature.h" void Init_hbsdctl(void) diff --git a/ext/hbsdctl.rb/include/feature.h b/ext/hbsdctl.rb/include/feature.h new file mode 100644 index 0000000..ae07457 --- /dev/null +++ b/ext/hbsdctl.rb/include/feature.h @@ -0,0 +1,2 @@ +#include +VALUE feature_set(VALUE, VALUE, VALUE); diff --git a/ext/hbsdctl.rb/include/ffi.h b/ext/hbsdctl.rb/include/ffi.h new file mode 100644 index 0000000..f42f344 --- /dev/null +++ b/ext/hbsdctl.rb/include/ffi.h @@ -0,0 +1,4 @@ +#include +VALUE ffi_library_version(VALUE); +VALUE ffi_available_features(VALUE); +VALUE ffi_reset(VALUE, VALUE, VALUE);