Skip to content

Commit

Permalink
Close /dev/mem file descriptor when not needed anymore
Browse files Browse the repository at this point in the history
Signed-off-by: Andi Kleen <ak@linux.intel.com>
  • Loading branch information
Andi Kleen committed Dec 4, 2009
1 parent b48e9d1 commit 16f800a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
19 changes: 18 additions & 1 deletion dmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct anchor {
} __attribute__((packed));

static struct dmi_entry *entries;
static int entrieslen;
static int numentries;
static int dmi_length;
static struct dmi_entry **handle_to_entry;
Expand Down Expand Up @@ -175,7 +176,8 @@ int opendmi(void)
printf("DMI tables at %x, %u bytes, %u entries\n",
a->table, a->length, a->numentries);
corr = a->table - round_down(a->table, pagesize);
entries = mmap(NULL, round_up(a->length + pagesize, pagesize),
entrieslen = round_up(a->length + pagesize, pagesize);
entries = mmap(NULL, entrieslen,
PROT_READ, MAP_SHARED, memfd,
round_down(a->table, pagesize));
if (entries == (struct dmi_entry *)-1) {
Expand Down Expand Up @@ -526,3 +528,18 @@ void checkdmi(void)
if (!dmi_forced)
do_dmi = dmi_sanity_check();
}

#define FREE(x) free(x), (x) = NULL

void closedmi(void)
{
if (!entries)
return;
munmap(entries, entrieslen);
entries = NULL;
FREE(dmi_dimms);
FREE(dmi_arrays);
FREE(dmi_ranges);
FREE(dmi_array_ranges);
FREE(handle_to_entry);
}
1 change: 1 addition & 0 deletions dmi.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void dmi_set_verbosity(int v);

char *dmi_getstring(struct dmi_entry *e, unsigned number);
extern void checkdmi(void);
void closedmi(void);

/* valid after opendmi: */
extern struct dmi_memdev **dmi_dimms;
Expand Down
2 changes: 2 additions & 0 deletions mcelog.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,8 @@ int main(int ac, char **av)
if (daemon_mode) {
check_cpu();
prefill_memdb();
if (!do_dmi)
closedmi();
server_setup();
page_setup();
drop_cred();
Expand Down

0 comments on commit 16f800a

Please sign in to comment.