diff --git a/ext/hbsdctl.rb/hbsdctl.c b/ext/hbsdctl.rb/hbsdctl.c index e0f31fd..ef39479 100644 --- a/ext/hbsdctl.rb/hbsdctl.c +++ b/ext/hbsdctl.rb/hbsdctl.c @@ -1,5 +1,15 @@ #include #include +#include + +static VALUE +bsd_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) @@ -41,6 +51,9 @@ feature_set(VALUE self, VALUE path, VALUE state) char *cpath; VALUE name; + if (getuid() != 0) { + rb_raise(bsd_eError(), "This operation requires root privileges."); + } Check_Type(path, T_STRING); Check_Type(state, T_FIXNUM); cpath = RSTRING_PTR(path); @@ -54,10 +67,7 @@ feature_set(VALUE self, VALUE path, VALUE state) if (r == 0) { return Qtrue; } else { - 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")); - rb_raise(rb_eError, "hbsdcontrol_set_feature_state failed"); + rb_raise(bsd_eError(), "hbsdcontrol_set_feature_state failed"); } }