]>
git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod.c
2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011 ProFUSION embedded systems
5 * Copyright (C) 2011 Lucas De Marchi <lucas.de.marchi@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation version 2.1.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "libkmod-private.h"
35 * @short_description: libkmod context
37 * The context contains the default values for the library user,
38 * and is passed to all library operations.
44 * Opaque object representing the library context.
48 void (*log_fn
)(struct kmod_ctx
*ctx
,
49 int priority
, const char *file
, int line
, const char *fn
,
50 const char *format
, va_list args
);
55 void kmod_log(struct kmod_ctx
*ctx
,
56 int priority
, const char *file
, int line
, const char *fn
,
57 const char *format
, ...)
61 va_start(args
, format
);
62 ctx
->log_fn(ctx
, priority
, file
, line
, fn
, format
, args
);
66 static void log_stderr(struct kmod_ctx
*ctx
,
67 int priority
, const char *file
, int line
, const char *fn
,
68 const char *format
, va_list args
)
70 fprintf(stderr
, "libkmod: %s: ", fn
);
71 vfprintf(stderr
, format
, args
);
76 * @ctx: kmod library context
78 * Retrieve stored data pointer from library context. This might be useful
79 * to access from callbacks like a custom logging function.
81 * Returns: stored userdata
83 KMOD_EXPORT
void *kmod_get_userdata(struct kmod_ctx
*ctx
)
92 * @ctx: kmod library context
93 * @userdata: data pointer
95 * Store custom @userdata in the library context.
97 KMOD_EXPORT
void kmod_set_userdata(struct kmod_ctx
*ctx
, void *userdata
)
101 ctx
->userdata
= userdata
;
104 static int log_priority(const char *priority
)
109 prio
= strtol(priority
, &endptr
, 10);
110 if (endptr
[0] == '\0' || isspace(endptr
[0]))
112 if (strncmp(priority
, "err", 3) == 0)
114 if (strncmp(priority
, "info", 4) == 0)
116 if (strncmp(priority
, "debug", 5) == 0)
124 * Create kmod library context. This reads the kmod configuration
125 * and fills in the default values.
127 * The initial refcount is 1, and needs to be decremented to
128 * release the resources of the kmod library context.
130 * Returns: a new kmod library context
132 KMOD_EXPORT
int kmod_new(struct kmod_ctx
**ctx
)
137 c
= calloc(1, sizeof(struct kmod_ctx
));
142 c
->log_fn
= log_stderr
;
143 c
->log_priority
= LOG_ERR
;
145 /* environment overwrites config */
146 env
= getenv("KMOD_LOG");
148 kmod_set_log_priority(c
, log_priority(env
));
150 info(c
, "ctx %p created\n", c
);
151 dbg(c
, "log_priority=%d\n", c
->log_priority
);
158 * @ctx: kmod library context
160 * Take a reference of the kmod library context.
162 * Returns: the passed kmod library context
164 KMOD_EXPORT
struct kmod_ctx
*kmod_ref(struct kmod_ctx
*ctx
)
174 * @ctx: kmod library context
176 * Drop a reference of the kmod library context. If the refcount
177 * reaches zero, the resources of the context will be released.
180 KMOD_EXPORT
struct kmod_ctx
*kmod_unref(struct kmod_ctx
*ctx
)
185 if (ctx
->refcount
> 0)
187 info(ctx
, "context %p released\n", ctx
);
194 * @ctx: kmod library context
195 * @log_fn: function to be called for logging messages
197 * The built-in logging writes to stderr. It can be
198 * overridden by a custom function, to plug log messages
199 * into the user's logging functionality.
202 KMOD_EXPORT
void kmod_set_log_fn(struct kmod_ctx
*ctx
,
203 void (*log_fn
)(struct kmod_ctx
*ctx
,
204 int priority
, const char *file
,
205 int line
, const char *fn
,
206 const char *format
, va_list args
))
208 ctx
->log_fn
= log_fn
;
209 info(ctx
, "custom logging function %p registered\n", log_fn
);
213 * kmod_get_log_priority:
214 * @ctx: kmod library context
216 * Returns: the current logging priority
218 KMOD_EXPORT
int kmod_get_log_priority(struct kmod_ctx
*ctx
)
220 return ctx
->log_priority
;
224 * kmod_set_log_priority:
225 * @ctx: kmod library context
226 * @priority: the new logging priority
228 * Set the current logging priority. The value controls which messages
231 KMOD_EXPORT
void kmod_set_log_priority(struct kmod_ctx
*ctx
, int priority
)
233 ctx
->log_priority
= priority
;
236 struct kmod_list_entry
;
237 struct kmod_list_entry
*kmod_list_entry_get_next(struct kmod_list_entry
*list_entry
);
238 const char *kmod_list_entry_get_name(struct kmod_list_entry
*list_entry
);
239 const char *kmod_list_entry_get_value(struct kmod_list_entry
*list_entry
);
242 struct kmod_ctx
*ctx
;
246 KMOD_EXPORT
struct kmod_thing
*kmod_thing_ref(struct kmod_thing
*thing
)
254 KMOD_EXPORT
struct kmod_thing
*kmod_thing_unref(struct kmod_thing
*thing
)
259 if (thing
->refcount
> 0)
261 dbg(thing
->ctx
, "context %p released\n", thing
);
266 KMOD_EXPORT
struct kmod_ctx
*kmod_thing_get_ctx(struct kmod_thing
*thing
)
271 KMOD_EXPORT
int kmod_thing_new_from_string(struct kmod_ctx
*ctx
, const char *string
, struct kmod_thing
**thing
)
273 struct kmod_thing
*t
;
275 t
= calloc(1, sizeof(struct kmod_thing
));
285 KMOD_EXPORT
struct kmod_list_entry
*kmod_thing_get_some_list_entry(struct kmod_thing
*thing
)