- int callback_id = clua_store_callback(L, 1);
- if (callback_id < 0)
- return luaL_error(L, "%s", "Storing callback function failed");
-
- lua_State *thread = lua_newthread(L);
- if (thread == NULL)
- return luaL_error(L, "%s", "lua_newthread failed");
- clua_store_thread(L, -1);
- lua_pop(L, 1);
-
- clua_callback_data_t *cb = calloc(1, sizeof(*cb));
- if (cb == NULL)
- return luaL_error(L, "%s", "calloc failed");
-
- cb->lua_state = thread;
- cb->callback_id = callback_id;
- cb->lua_function_name = strdup(function_name);
- pthread_mutex_init(&cb->lock, NULL);
-
- int status = plugin_register_complex_read(/* group = */ "lua",
- /* name = */ function_name,
- /* callback = */ clua_read,
- /* interval = */ 0,
- &(user_data_t){
- .data = cb,
- });
-
- if (status != 0)
- return luaL_error(L, "%s", "plugin_register_complex_read failed");
- return 0;
-} /* }}} int lua_cb_register_read */
-
-static int lua_cb_register_write(lua_State *L) /* {{{ */
-{
- int nargs = lua_gettop(L);
-
- if (nargs != 1)
- return luaL_error(L, "Invalid number of arguments (%d != 1)", nargs);
+ lua_getglobal(L, fname); // Push function into stack
+ lua_remove(L, 1); // Remove string from stack
+ if (!lua_isfunction(L, -1)) {
+ return luaL_error(L, "Unable to find function '%s'", fname);
+ }
+ } else {
+ lua_getfield(L, LUA_REGISTRYINDEX, "collectd:callback_num");
+ int tmp = lua_tointeger(L, -1);
+ snprintf(subname, sizeof(subname), "callback_%d", tmp);
+ lua_pop(L, 1); // Remove old value from stack
+ lua_pushinteger(L, tmp + 1);
+ lua_setfield(L, LUA_REGISTRYINDEX, "collectd:callback_num"); // pops value
+ }