Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit c9a283c2f314e0a894e5dda1beefc825a8ae513b @ditesh committed Jun 14, 2011
Showing with 177 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +5 −0 README
  3. +141 −0 flock.cc
  4. +17 −0 package.json
  5. +12 −0 wscript
@@ -0,0 +1,2 @@
+.*.sw?
+.lock-wscript
5 README
@@ -0,0 +1,5 @@
+Native asynchronous flock()-ing mechanism for Node.js.
+
+Tested on NodeJS 0.4.8, with on Linux (Fedora 15).
+
+See demo/demo.js to see it in action!
141 flock.cc
@@ -0,0 +1,141 @@
+#include <v8.h>
+#include <node.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/file.h>
+#include <sys/stat.h> /* for S_* constants */
+#include <string.h> /* for strerror(3) prototype */
+#include <stdio.h> /* for fprintf(3),printf(3),stderr protype */
+#include <errno.h> /* for errno prototype */
+#include <unistd.h> /* for close(2) prototypes */
+#define FILENAME "/tmp/flock.example"
+
+#define REQ_FUN_ARG(I, VAR) \
+ if (args.Length() <= (I) || !args[I]->IsFunction()) \
+ return ThrowException(Exception::TypeError( \
+ String::New("Argument " #I " must be a function"))); \
+ Local<Function> VAR = Local<Function>::Cast(args[I]);
+
+
+using namespace node;
+using namespace v8;
+
+class UNIX:ObjectWrap {
+
+public:
+
+ static Persistent<FunctionTemplate> s_ct;
+ static void Init(Handle<Object> target) {
+
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(New);
+
+ s_ct = Persistent<FunctionTemplate>::New(t);
+ s_ct->InstanceTemplate()->SetInternalFieldCount(1);
+ s_ct->SetClassName(String::NewSymbol("PAM"));
+
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "flock", flock);
+ target->Set(String::NewSymbol("UNIX"), s_ct->GetFunction());
+
+ }
+
+ ~PAM() {}
+
+ static Handle<Value> New(const Arguments& args) {
+
+ HandleScope scope;
+ UNIX* hw = new UNIX();
+ hw->Wrap(args.This());
+ return args.This();
+
+ }
+
+ struct baton_t {
+ UNIX *hw;
+ int fd;
+ bool result;
+ Persistent<Function> cb;
+ };
+
+ static Handle<Value> authenticate(const Arguments& args) {
+
+ HandleScope scope;
+ REQ_FUN_ARG(2, cb);
+
+ UNIX* hw = ObjectWrap::Unwrap<UNIX>(args.This());
+ baton_t *baton = new baton_t();
+ baton->hw = hw;
+ baton->fd = args[0];
+ baton->result = false;
+
+ hw->Ref();
+
+ eio_custom(EIO_flock, EIO_PRI_DEFAULT, EIO_AfterPam, baton);
+ ev_ref(EV_DEFAULT_UC);
+
+ return Undefined();
+
+ }
+
+ static int EIO_flock(eio_req *req) {
+
+ bool result = false;
+ struct baton_t* args = (struct baton_t *) req->data;
+ int fd = args->fd;
+
+ if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
+
+ baton->result = true;
+ printf("Lock succeeded");
+
+ } else {
+
+ printf("Lock did not succeed");
+
+ }
+
+ return 0;
+
+ }
+
+ static int EIO_AfterPam(eio_req *req) {
+
+ HandleScope scope;
+ baton_t *baton = static_cast<baton_t *>(req->data);
+ ev_unref(EV_DEFAULT_UC);
+ baton->hw->Unref();
+
+ Local<Value> argv[1];
+
+ // This doesn't work
+ //argv[0] = False();
+
+ // This works, but this is not what we want
+ argv[0] = Integer::New(baton->result);
+
+ TryCatch try_catch;
+
+ baton->cb->Call(Context::GetCurrent()->Global(), 1, argv);
+
+ if (try_catch.HasCaught())
+ FatalException(try_catch);
+
+ baton->cb.Dispose();
+
+ delete baton;
+ return 0;
+
+ }
+};
+
+Persistent<FunctionTemplate> PAM::s_ct;
+
+extern "C" {
+
+ static void init (Handle<Object> target) {
+ PAM::Init(target);
+ }
+
+ NODE_MODULE(flock, init);
+}
@@ -0,0 +1,17 @@
+{
+ "name" : "pam",
+ "version" : "v0.1.0",
+ "description" : "Native Node.js C++ extension for authenticating off PAM.",
+ "homepage" : "https://github.com/ditesh/node-pam",
+ "author" : {
+ "name" : "Ditesh Shashikant Gathani",
+ "email" : "ditesh@gathani.org",
+ "url" : "http://ditesh.gathani.org/blog/"
+ },
+ "repository" : { "type" : "git", "url" : "https://github.com/ditesh/node-pam.git" },
+ "scripts" : {
+ "preinstall" : "node-waf configure && node-waf build",
+ "preuninstall" : "rm -rf build/*"
+ },
+ "main" : "build/default/pam.node"
+}
12 wscript
@@ -0,0 +1,12 @@
+def set_options(opt):
+ opt.tool_options("compiler_cxx")
+
+def configure(conf):
+ conf.check_tool("compiler_cxx")
+ conf.check_tool("node_addon")
+
+def build(bld):
+ obj = bld.new_task_gen("cxx", "shlib", "node_addon")
+ obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"]
+ obj.target = "flock"
+ obj.source = "flock.cc"

0 comments on commit c9a283c

Please sign in to comment.