FreeBSD Bugzilla – Attachment 215243 Details for
Bug 246981
ports-mgmt/pkg plugins: configuration settings not provided due to missing object allocation
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Updated examples in pkg-plugins repository
pkg-plugins.diff (text/plain), 17.55 KB, created by
Markus Stoff
on 2020-06-05 08:21:46 UTC
(
hide
)
Description:
Updated examples in pkg-plugins repository
Filename:
MIME Type:
Creator:
Markus Stoff
Created:
2020-06-05 08:21:46 UTC
Size:
17.55 KB
patch
obsolete
>diff --git a/pkg-plugin-template/Makefile b/pkg-plugin-template/Makefile >index bdced65..8f0a460 100644 >--- a/pkg-plugin-template/Makefile >+++ b/pkg-plugin-template/Makefile >@@ -1,18 +1,17 @@ > .include <bsd.own.mk> > >-LIB= pkg-plugin-template >-INCS= template.h >-#WARNS= 6 > PREFIX?= /usr/local >-LIBDIR= ${PREFIX}/lib >-INCLUDEDIR= ${PREFIX}/include >-SHLIB_MAJOR= 0 >+LIBDIR= ${PREFIX}/lib/pkg/ >+SHLIB_DIR?= ${LIBDIR}/ >+SHLIB_NAME?= ${PLUGIN_NAME}.so > >+PLUGIN_NAME= template > SRCS= template.c > >-CFLAGS+= -std=c99 -fPIC -shared >-CFLAGS+= -I${INCLUDEDIR} >+PKGFLAGS!= pkgconf --cflags pkg >+CFLAGS+= ${PKGFLAGS} > >-DEBUG_FLAGS+= -g -O0 >+beforeinstall: >+ ${INSTALL} -d ${LIBDIR} > > .include <bsd.lib.mk> >diff --git a/pkg-plugin-template/README.md b/pkg-plugin-template/README.md >index 31d10a8..44cbafa 100644 >--- a/pkg-plugin-template/README.md >+++ b/pkg-plugin-template/README.md >@@ -16,25 +16,37 @@ Once the plugin is built you can install it using the following command: > > $ make install > >-The plugin will be installed as a shared library in ${PREFIX}/lib/libpkg-plugin-template.so >+The plugin will be installed as a shared library in ${PREFIX}/lib/pkg/template.so (or wherever >+the *PKG\_PLUGINS\_DIR* option points to). > > ## Configuring the plugin > > In order to configure the plugin simply copy the *template.conf* file to the pkgng plugins directory, >-which by default is set to */usr/local/etc/pkg/plugins*, unless you've specified it elsewhere by >-using the *PKG\_PLUGINS\_DIR* option. >+which by default is set to */usr/local/etc/pkg*, unless you've specified it elsewhere by >+using the *PLUGINS\_CONF\_DIR* option. > >- $ cp /path/to/pkg-plugins-template/template.conf /usr/local/etc/pkg/plugins/ >+ $ cp /path/to/pkg-plugins-template/template.conf /usr/local/etc/pkg/ > >+## Enabling the plugin >+ >+To use the plugin, tell pkgng about it by adding it to the *PLUGINS* option in ${PREFIX}/etc/pkg.conf. >+Also make sure *PKG\_ENABLE\_PLUGINS* is set to true: >+ >+ PKG_ENABLE_PLUGINS = true; >+ PLUGINS [ >+ "foo", >+ "template", >+ ] >+ > ## Testing the plugin > > To test the plugin, first check that it is recongnized and > loaded by pkgng by executing the `pkg plugins` command: > > $ pkg plugins >- NAME DESC VERSION LOADED >- foo Foo plugin for pkg 1.1 YES >- template Template plugin for pkgng 1.0 YES >+ NAME DESC VERSION >+ foo Foo plugin for pkg 1.1.0 >+ template Template plugin for pkgng 1.0.0 > > If the plugin shows up correctly then you are good to go! :) > >diff --git a/pkg-plugin-template/template.c b/pkg-plugin-template/template.c >index b94bc65..9f6a9ca 100644 >--- a/pkg-plugin-template/template.c >+++ b/pkg-plugin-template/template.c >@@ -30,11 +30,15 @@ > /* Include pkg */ > #include <pkg.h> > >-/* Include any plugin headers here */ >-#include "template.h" >+/* Define plugin name and configuration settings */ >+static const char PLUGIN_NAME[] = "template"; >+static const char CFG_TEXT[] = "text"; >+static const char CFG_COUNT[] = "count"; >+static const char CFG_FLAG[] = "flag"; >+static const char CFG_COLORS[] = "colors"; > >-/* Define the plugin name */ >-#define PLUGIN_NAME "template" >+/* Maintain a reference to ourself */ >+static struct pkg_plugin *self; > > /* > * The plugin *must* provide an init function that is called by the library. >@@ -47,7 +51,7 @@ > * > * The plugin's init function prototype should be in the following form: > * >- * int pkg_plugins_init_<plugin> (void); >+ * int pkg_plugins_init (void); > * > * No arguments are passed to the plugin's init function. > * >@@ -55,8 +59,40 @@ > * upon failure EPKG_FATAL ( > 0 ) is returned to the caller. > */ > int >-pkg_plugins_init_template(void) >+pkg_plugin_init(struct pkg_plugin *p) > { >+ /* Keep a reference to our plugin object, so it can be used inside the callback functions */ >+ self = p; >+ >+ /* >+ * Declare the plugin's metadata >+ * >+ * This information is shown by 'pkg plugins'. >+ * >+ */ >+ >+ pkg_plugin_set(p, PKG_PLUGIN_NAME, PLUGIN_NAME); >+ pkg_plugin_set(p, PKG_PLUGIN_DESC, "Template plugin for pkgng"); >+ pkg_plugin_set(p, PKG_PLUGIN_VERSION, "1.0.0"); >+ >+ /* >+ * Register configuration settings >+ * >+ * Declares >+ * - key (the name of the setting in the configuration file) >+ * - type (type of the setting, one of PKG_STRING, PKG_BOOL, PKG_INT, PKG_ARRAY) >+ * - default value (value if not set in the configuration file, provide empty string for no default) >+ * >+ */ >+ >+ pkg_plugin_conf_add(p, PKG_STRING, CFG_TEXT, ""); >+ pkg_plugin_conf_add(p, PKG_INT, CFG_COUNT, "0"); >+ pkg_plugin_conf_add(p, PKG_BOOL, CFG_FLAG, "false"); >+ pkg_plugin_conf_add(p, PKG_ARRAY, CFG_COLORS, ""); >+ >+ /* Parse the configuration file for above settings. Do not forget this! */ >+ pkg_plugin_parse(p); >+ > /* > * Register two functions for hooking into the library > * >@@ -68,15 +104,15 @@ pkg_plugins_init_template(void) > * > */ > >- /* printf(">>> Plugin '%s' is about to hook into pkgng.. yay! :)\n", PLUGIN_NAME); */ >+ /* printf(">>> Plugin '%s' is about to hook into pkgng.. yay! :)\n", pkg_plugin_get(p, PKG_PLUGIN_NAME)); */ > >- if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGINS_HOOK_PRE_INSTALL, &my_callback1) != EPKG_OK) { >- fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME); >+ if (pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_PRE_INSTALL, &my_callback1) != EPKG_OK) { >+ pkg_plugin_error(p, "failed to hook into the library"); > return (EPKG_FATAL); > } > >- if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGINS_HOOK_POST_INSTALL, &my_callback2) != EPKG_OK) { >- fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME); >+ if (pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_POST_INSTALL, &my_callback2) != EPKG_OK) { >+ pkg_plugin_error(p, "failed to hook into the library"); > return (EPKG_FATAL); > } > >@@ -93,15 +129,15 @@ pkg_plugins_init_template(void) > * > * The plugin's shutdown function prototype should be in the following form: > * >- * int pkg_plugins_shutdown_<plugin> (void); >+ * int pkg_plugins_shutdown (struct pkg_plugin *); > * > * Upon successful shutdown of the plugin EPKG_OK (0) is returned and > * upon failure EPKG_FATAL ( > 0 ) is returned to the caller. > */ > int >-pkg_plugins_shutdown_template(void) >+pkg_plugin_shutdown(struct pkg_plugin *p __unused) > { >- /* printf(">>> Plugin '%s' is shutting down, enough working for today.. :)\n", PLUGIN_NAME); */ >+ /* printf(">>> Plugin '%s' is shutting down, enough working for today.. :)\n", pkg_plugin_get(p, PKG_PLUGIN_NAME)); */ > > /* > * Perform any cleanup if needed, e.g.: >@@ -120,8 +156,11 @@ pkg_plugins_shutdown_template(void) > * And now we need to define our workers, > * the plugin functions that carry out the real work. > * >- * A plugin callback function accepts only one argument and >- * should return EPKG_OK (0) on success and EPKG_FATAL ( > 0 ) on failure. >+ * A plugin callback function must satisfy the following function pointer signature: >+ * >+ * int(*pkg_plugin_callback)(void *data, struct pkgdb *db); >+ * >+ * It should return EPKG_OK (0) on success and EPKG_FATAL ( > 0 ) on failure. > * > * Plugin callbacks must also take care of proper casting of the (void *data) argument. > * >@@ -130,16 +169,47 @@ pkg_plugins_shutdown_template(void) > * > * For example if a plugin hooks into PKG_PLUGINS_HOOK_PRE_INSTALL the (void *data) passed to the > * called is (struct pkg_jobs *), so the plugin callback must cast it explicitely. >+ * >+ * If the callback needs to access configuration data, a reference to the plugin object >+ * can be kept in the global scope of this compilation unit: >+ * >+ * static struct pkg_plugin *self; >+ * > */ > int > my_callback1(void *data, struct pkgdb *db) > { >- printf("Hey, I was just called by the library, lets see what we've got here..\n"); >+ const char *text = NULL; >+ int64_t count = 0; >+ bool flag = false; >+ const pkg_object *colors = NULL; >+ const pkg_object *color = NULL; >+ const pkg_object *cfg = NULL; >+ pkg_iter it = NULL; >+ >+ /* Get configuration object */ >+ cfg = pkg_plugin_conf(self); >+ >+ /* Get configuration data */ >+ flag = pkg_object_bool(pkg_object_find(cfg, CFG_FLAG)); >+ count = pkg_object_int(pkg_object_find(cfg, CFG_COUNT)); >+ text = pkg_object_string(pkg_object_find(cfg, CFG_TEXT)); >+ colors = pkg_object_find(cfg, CFG_COLORS); >+ >+ >+ pkg_plugin_info(self, "Hey, I was just called by the library, lets see what we've got here.."); >+ >+ pkg_plugin_info(self, " Text: '%s'", text); >+ pkg_plugin_info(self, " Count: %i", count); >+ pkg_plugin_info(self, " Flag: %s", flag ? "true" : "false"); >+ while ((color = pkg_object_iterate(colors, &it)) != NULL) { >+ pkg_plugin_info(self, " Color: %s", pkg_object_string(color)); >+ } > > if (data == NULL) >- printf("Hmm.. no data for me today, guess I'll just go and grab a mohito then..\n"); >+ pkg_plugin_info(self, "Hmm.. no data for me today, guess I'll just go and grab a mohito then.."); > else >- printf("Got some data.. okay, okay.. I'll do something useful then..\n"); >+ pkg_plugin_info(self, "Got some data.. okay, okay.. I'll do something useful then.."); > > return (EPKG_OK); > } >@@ -150,12 +220,12 @@ my_callback1(void *data, struct pkgdb *db) > int > my_callback2(void *data, struct pkgdb *db) > { >- printf("Hey, I was just called again, lets see what its all about this time..\n"); >+ pkg_plugin_info(self, "Hey, I was just called again, lets see what its all about this time.."); > > if (data == NULL) >- printf("Hmm.. no data, no work.. today is my lucky day!\n"); >+ pkg_plugin_info(self, "Hmm.. no data, no work.. today is my lucky day!"); > else >- printf("Work never ends.. I'll do something useful again..\n"); >+ pkg_plugin_info(self, "Work never ends.. I'll do something useful again.."); > > return (EPKG_OK); > } >diff --git a/pkg-plugin-template/template.conf b/pkg-plugin-template/template.conf >index 674c8b9..34f599f 100644 >--- a/pkg-plugin-template/template.conf >+++ b/pkg-plugin-template/template.conf >@@ -1,26 +1,21 @@ >-# Configuration file for pkg-plugin-template >+# Configuration file for pkg plugin: template >+# >+# The file is in UCL format. For more information on the syntax of UCL, >+# please visit the official UCL website: http://github.com/vstakhov/libucl >+# >+# Also see pkg.conf(5) for general details on using plugins for pkg(8). >+# >+ >+# String value >+text = "Simple Text" >+# Integer value >+count = 5 >+# Boolean value >+flag = true >+# List of strings >+colors [ >+ "red", >+ "green", >+ "blue", >+] > >-### Start of required plugin options ### >- >-# Plugin is enabled when one of 'YES', 'TRUE' or 'ON' is specified >-enabled=YES >- >-# Name of the plugin, used to identify the plugin to the library >-name=template >- >-# A short description of the plugin >-description=Template plugin for pkgng >- >-# Plugin version >-version=1.0 >- >-# Path to the plugin file >-plugin=/usr/local/lib/libpkg-plugin-template.so >- >-### End of required plugin options ### >- >-# Plugins can also use this file for putting plugin specific configurations >-# For example a plugin could have the following options specified if needed and being used by the plugin >-# zfs_pool=zroot >-# zfs_snapshots_recursive=YES >-# ... >\ No newline at end of file >diff --git a/pkg-plugin-template/template.h b/pkg-plugin-template/template.h >deleted file mode 100644 >index f37d541..0000000 >--- a/pkg-plugin-template/template.h >+++ /dev/null >@@ -1,38 +0,0 @@ >-/* >- * Copyright (c) 2012 Marin Atanasov Nikolov <dnaeon@gmail.com> >- * All rights reserved. >- * >- * Redistribution and use in source and binary forms, with or without >- * modification, are permitted provided that the following conditions >- * are met: >- * 1. Redistributions of source code must retain the above copyright >- * notice, this list of conditions and the following disclaimer >- * in this position and unchanged. >- * 2. Redistributions in binary form must reproduce the above copyright >- * notice, this list of conditions and the following disclaimer in the >- * documentation and/or other materials provided with the distribution. >- * >- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR >- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES >- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. >- * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, >- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT >- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, >- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY >- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT >- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF >- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. >- */ >- >-#ifndef _PKG_PLUGINS_TEMPLATE_H >-#define _PKG_PLUGINS_TEMPLATE_H >- >-/* callback functions */ >-int my_callback1(void *data, struct pkgdb *db); >-int my_callback2(void *data, struct pkgdb *db); >- >-/* plugin init and shutdown functions */ >-int pkg_plugins_init_template(void); >-int pkg_plugins_shutdown_template(void); >- >-#endif /* !_PKG_PLUGINS_TEMPLATE_H */ >diff --git a/stats/stats.c b/stats/stats.c >index 4b877a6..875f379 100644 >--- a/stats/stats.c >+++ b/stats/stats.c >@@ -39,7 +39,7 @@ static char version[] = "1.0.0"; > static char description[] = "Plugin for displaying package stats"; > static int plugin_stats_callback(void *data, struct pkgdb *db); > >-struct pkg_plugin *self; >+static struct pkg_plugin *self; > > int > pkg_plugin_init(struct pkg_plugin *p) >@@ -98,7 +98,7 @@ plugin_stats_callback(void *data, struct pkgdb *db) > > flatsize = pkgdb_stats(db, PKG_STATS_LOCAL_SIZE); > humanize_number(size, sizeof(flatsize), flatsize, "B", HN_AUTOSCALE, 0); >- pkg_plugin_info(self, "Installed packages : %" PRId64 " | Disk space: %s <<<\n", >+ pkg_plugin_info(self, "Installed packages : %" PRId64 " | Disk space: %s <<<", > pkgdb_stats(db, PKG_STATS_LOCAL_COUNT), > size); > >diff --git a/zfssnap/zfssnap.c b/zfssnap/zfssnap.c >index d1dc849..ce4dee5 100644 >--- a/zfssnap/zfssnap.c >+++ b/zfssnap/zfssnap.c >@@ -46,18 +46,15 @@ > #define PLUGIN_NAME "zfssnap" > extern char **environ; > >-enum { >- ZFS_FS=1, >- ZFS_PREFIX, >- ZFS_RECURSIVE, >-}; >+/* Define names of configuration settings */ >+static const char ZFS_DATASETS[] = "ZFS_DATASETS"; >+static const char ZFS_PREFIX[] = "ZFS_PREFIX"; >+static const char ZFS_RECURSIVE[] = "ZFS_RECURSIVE"; > >-struct pkg_plugin *self; >+static struct pkg_plugin *self; > > static int plugins_zfssnap_callback(void *data, struct pkgdb *db); > >-static int plugins_zfssnap_fd = -1; >- > int > pkg_plugin_init(struct pkg_plugin *p) > { >@@ -66,9 +63,9 @@ pkg_plugin_init(struct pkg_plugin *p) > pkg_plugin_set(p, PKG_PLUGIN_DESC, "ZFS snapshot plugin"); > pkg_plugin_set(p, PKG_PLUGIN_VERSION, "1.0.0"); > >- pkg_plugin_conf_add_list(p, ZFS_FS, "ZFS_FS"); >- pkg_plugin_conf_add_string(p, ZFS_PREFIX, "ZFS_PREFIX", "pkgsnap"); >- pkg_plugin_conf_add_bool(p, ZFS_RECURSIVE, "ZFS_RECURSIVE", false); >+ pkg_plugin_conf_add(p, PKG_ARRAY, ZFS_DATASETS, ""); >+ pkg_plugin_conf_add(p, PKG_STRING, ZFS_PREFIX, "pkgsnap"); >+ pkg_plugin_conf_add(p, PKG_BOOL, ZFS_RECURSIVE, "false"); > > pkg_plugin_parse(p); > >@@ -88,8 +85,6 @@ pkg_plugin_init(struct pkg_plugin *p) > int > pkg_plugin_shutdown(struct pkg_plugin *p __unused) > { >- close(plugins_zfssnap_fd); >- > return (EPKG_OK); > } > >@@ -98,7 +93,8 @@ plugins_zfssnap_callback(void *data, struct pkgdb *db) > { > char snap[MAXPATHLEN]; > struct tm *tm = NULL; >- struct pkg_config_value *zfs_fs; >+ const pkg_object *zfs_datasets = NULL; >+ const pkg_object *zfs_dataset = NULL; > bool zfs_recursive = false; > const char *zfs_prefix = NULL; > time_t t = 0; >@@ -111,6 +107,8 @@ plugins_zfssnap_callback(void *data, struct pkgdb *db) > NULL, > NULL, > }; >+ pkg_iter it = NULL; >+ const pkg_object *cfg = NULL; > > t = time(NULL); > tm = localtime(&t); >@@ -119,12 +117,14 @@ plugins_zfssnap_callback(void *data, struct pkgdb *db) > /* assert(db != NULL); */ > /* assert(data != NULL); */ > >- pkg_plugin_conf_bool(self, ZFS_RECURSIVE, &zfs_recursive); >- pkg_plugin_conf_string(self, ZFS_PREFIX, &zfs_prefix); >+ cfg = pkg_plugin_conf(self); >+ zfs_recursive = pkg_object_bool(pkg_object_find(cfg, ZFS_RECURSIVE)); >+ zfs_prefix = pkg_object_string(pkg_object_find(cfg, ZFS_PREFIX)); >+ zfs_datasets = pkg_object_find(cfg, ZFS_DATASETS); > >- while (pkg_plugin_conf_list(self, ZFS_FS, &zfs_fs) == EPKG_OK) { >+ while ((zfs_dataset = pkg_object_iterate(zfs_datasets, &it)) != NULL) { > snprintf(snap, sizeof(snap), "%s@%s-%d-%d-%d_%d.%d.%d", >- pkg_config_value(zfs_fs), zfs_prefix, >+ pkg_object_string(zfs_dataset), zfs_prefix, > 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, > tm->tm_hour, tm->tm_min, tm->tm_sec); > >@@ -135,8 +135,8 @@ plugins_zfssnap_callback(void *data, struct pkgdb *db) > argv[2] = snap; > argv[3] = NULL; > } >- pkg_plugin_info(self, "Creating ZFS snapshot: %s", snap); >- if ((error = posix_spawn(&pid, "/sbin/zfs", NULL, NULL, >+ pkg_plugin_info(self, "Creating%s ZFS snapshot: %s", zfs_recursive ? " recursive" : "", snap); >+ if ((error = posix_spawn(&pid, "/openzfs/sbin/zfs", NULL, NULL, > __DECONST(char **, argv), environ)) != 0) { > errno = error; > pkg_plugin_errno(self, "Failed to snapshot", snap); >diff --git a/zfssnap/zfssnap.conf b/zfssnap/zfssnap.conf >index dcfc1d1..a0d8e76 100644 >--- a/zfssnap/zfssnap.conf >+++ b/zfssnap/zfssnap.conf >@@ -2,14 +2,14 @@ > # zfssnap specific options follow below > # > >-# ZFS file system >-zfs_fs=zroot >- >-# ZFS args, '-r' for recursive snapshots creation >-zfs_args=-r >- >-# ZFS snapshot prefix name >-zfs_prefix=zfssnap >+# ZFS datasets >+ZFS_DATASETS = [ >+ "zroot/ROOT/default", >+] > >+# ZFS recursive snapshots creation (default: false) >+#ZFS_RECURSIVE = false > >+# ZFS snapshot prefix name (default: "pkgsnap") >+#ZFS_PREFIX = "pkgsnap" >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 246981
:
215227
|
215228
|
215229
| 215243