summaryrefslogtreecommitdiffstats
path: root/udev-block-notify.c
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2013-07-04 17:38:03 +0200
committerGravatar Christian Hesse <mail@eworm.de>2013-07-04 17:38:03 +0200
commit2f015dcd2204f080dbfed2c8f9ca3568bdcde41f (patch)
tree826631c4f092349cf97f031aa793802b5e9fd09e /udev-block-notify.c
parent4cb7f2203bbbb6d45516f7d3d6f7a27caf156fa6 (diff)
downloadudev-block-notify-2f015dcd2204f080dbfed2c8f9ca3568bdcde41f.tar.gz
udev-block-notify-2f015dcd2204f080dbfed2c8f9ca3568bdcde41f.tar.zst
fix memory management
Diffstat (limited to 'udev-block-notify.c')
-rw-r--r--udev-block-notify.c39
1 files changed, 27 insertions, 12 deletions
diff --git a/udev-block-notify.c b/udev-block-notify.c
index 0db489d..fccd252 100644
--- a/udev-block-notify.c
+++ b/udev-block-notify.c
@@ -59,12 +59,14 @@ int main (int argc, char ** argv) {
const char *value = NULL;
fd_set readfds;
GError *error = NULL;
- int devnum, errcount = 0;
- NotifyNotification ***notification;
+ NotifyNotification ***notification = NULL;
+ int errcount = 0;
+ dev_t devnum = 0;
+ unsigned int major = 0, minor = 0, maxmajor = 0;
+ unsigned int *maxminor = NULL;
struct udev_device *dev = NULL;
struct udev_monitor *mon = NULL;
struct udev *udev = NULL;
- unsigned short int i, j, major, minor;
printf("%s: %s v%s (compiled: " __DATE__ ", " __TIME__
#if DEBUG
@@ -87,13 +89,6 @@ int main (int argc, char ** argv) {
udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL);
udev_monitor_enable_receiving(mon);
- notification = malloc(256 * sizeof(NotifyNotification));
- for(i = 0; i < 256; i++) {
- notification[i] = malloc(256 * sizeof(NotifyNotification));
- for(j = 0; j < 256; j++)
- notification[i][j] = NULL;
- }
-
while (1) {
FD_ZERO(&readfds);
if (mon != NULL)
@@ -106,8 +101,28 @@ int main (int argc, char ** argv) {
if(dev) {
device = (char *) udev_device_get_sysname(dev);
devnum = udev_device_get_devnum(dev);
- major = devnum / 256;
- minor = devnum - (major * 256);
+ major = major(devnum);
+ minor = minor(devnum);
+
+ /* make sure we have allocated memory */
+ if (maxmajor <= major) {
+ notification = realloc(notification, (major + 1) * sizeof(size_t));
+ maxminor = realloc(maxminor, (major + 1) * sizeof(unsigned int));
+ while (maxmajor <= major) {
+ notification[maxmajor] = NULL;
+ maxminor[maxmajor] = 0;
+ maxmajor++;
+ }
+ maxmajor--;
+ }
+ if (maxminor[major] <= minor) {
+ notification[major] = realloc(notification[major], (minor + 1) * sizeof(size_t));
+ while (maxminor[major] <= minor) {
+ notification[major][maxminor[major]] = NULL;
+ maxminor[major]++;
+ }
+ maxminor[major]--;
+ }
action = udev_device_get_action(dev)[0];
switch(action) {