Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make libdmi and implement C API #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CFLAGS += -W -Wall -Wshadow -Wstrict-prototypes -Wpointer-arith -Wcast-qual \
-Wcast-align -Wwrite-strings -Wmissing-prototypes -Winline -Wundef

# Let lseek and mmap support 64-bit wide offsets
CFLAGS += -D_FILE_OFFSET_BITS=64
CFLAGS += -D_FILE_OFFSET_BITS=64 -fPIC

#CFLAGS += -DBIGENDIAN
#CFLAGS += -DALIGNMENT_WORKAROUND
Expand All @@ -31,11 +31,12 @@ CFLAGS += -D_FILE_OFFSET_BITS=64
LDFLAGS ?=

DESTDIR =
prefix = /usr/local
prefix = /usr
sbindir = $(prefix)/sbin
mandir = $(prefix)/share/man
man8dir = $(mandir)/man8
docdir = $(prefix)/share/doc/dmidecode
libdmidir = /libdmi

INSTALL := install
INSTALL_DATA := $(INSTALL) -m 644
Expand All @@ -47,6 +48,9 @@ RM := rm -f
MACHINE ?= $(shell uname -m 2>/dev/null)

# These programs are only useful on x86
LIB-FILES := libdmi.so libdmi.a
OBJS := libdmi.o dmiopt.o dmioem.o dmioutput.o util.o
LIB-HEADERS := libdmi.h types.h
PROGRAMS-i386 := biosdecode ownership vpddecode
PROGRAMS-i486 := $(PROGRAMS-i386)
PROGRAMS-i586 := $(PROGRAMS-i386)
Expand All @@ -56,14 +60,14 @@ PROGRAMS-amd64 := $(PROGRAMS-x86_64)

PROGRAMS := dmidecode $(PROGRAMS-$(MACHINE))

all : $(PROGRAMS)
all : $(PROGRAMS) libdmi.so libdmi.a

#
# Programs
#

dmidecode : dmidecode.o dmiopt.o dmioem.o dmioutput.o util.o
$(CC) $(LDFLAGS) dmidecode.o dmiopt.o dmioem.o dmioutput.o util.o -o $@
dmidecode : dmidecode.o libdmi.o dmiopt.o dmioem.o util.o dmioutput.o
$(CC) $(LDFLAGS) dmidecode.o libdmi.o dmiopt.o dmioem.o util.o dmioutput.o -o $@

biosdecode : biosdecode.o util.o
$(CC) $(LDFLAGS) biosdecode.o util.o -o $@
Expand All @@ -78,14 +82,18 @@ vpddecode : vpddecode.o vpdopt.o util.o
# Objects
#

dmidecode.o : dmidecode.c version.h types.h util.h config.h dmidecode.h \
libdmi.o : libdmi.c version.h types.h util.h config.h libdmi.h \
dmiopt.h dmioem.h dmioutput.h
$(CC) $(CFLAGS) -c $< -o $@

dmiopt.o : dmiopt.c config.h types.h util.h dmidecode.h dmiopt.h
dmidecode.o : main.c libdmi.c version.h types.h util.h config.h libdmi.h \
dmiopt.h dmioem.h
$(CC) $(CFLAGS) -c $< -o $@

dmioem.o : dmioem.c types.h dmidecode.h dmioem.h dmioutput.h
dmiopt.o : dmiopt.c config.h types.h util.h dmiopt.h
$(CC) $(CFLAGS) -c $< -o $@

dmioem.o : dmioem.c types.h dmioem.h
$(CC) $(CFLAGS) -c $< -o $@

dmioutput.o : dmioutput.c types.h dmioutput.h
Expand All @@ -106,16 +114,22 @@ vpdopt.o : vpdopt.c config.h util.h vpdopt.h
util.o : util.c types.h util.h config.h
$(CC) $(CFLAGS) -c $< -o $@

libdmi.so : $(OBJS)
$(CC) $(CFLAGS) -shared $^ -o $@

libdmi.a : $(OBJS)
ar rcs $@ $^

#
# Commands
#

strip : $(PROGRAMS)
strip $(PROGRAMS)

install : install-bin install-man install-doc
install : install-bin install-man install-doc install-lib

uninstall : uninstall-bin uninstall-man uninstall-doc
uninstall : uninstall-bin uninstall-man uninstall-doc uninstall-lib

install-bin : $(PROGRAMS)
$(INSTALL_DIR) $(DESTDIR)$(sbindir)
Expand All @@ -126,6 +140,20 @@ uninstall-bin :
for program in $(PROGRAMS) ; do \
$(RM) $(DESTDIR)$(sbindir)/$$program ; done

install-lib:
$(INSTALL_DIR) $(DESTDIR)$(prefix)/lib$(libdmidir)
$(INSTALL_DIR) $(DESTDIR)$(prefix)/include$(libdmidir)
for program in $(LIB-FILES) ; do \
$(INSTALL_PROGRAM) $$program $(DESTDIR)$(prefix)/lib$(libdmidir)/ ; done
for program in $(LIB-HEADERS) ; do \
$(INSTALL_PROGRAM) $$program $(DESTDIR)$(prefix)/include$(libdmidir)/ ; done

uninstall-lib:
for program in $(LIB-FILES) ; do \
$(RM) $(DESTDIR)$(prefix)/lib$(libdmidir)/$$program ; done
for program in $(LIB-HEADERS) ; do \
$(RM) $(DESTDIR)$(prefix)/include$(libdmidir)/$$program ; done

install-man :
$(INSTALL_DIR) $(DESTDIR)$(man8dir)
for program in $(PROGRAMS) ; do \
Expand All @@ -145,4 +173,4 @@ uninstall-doc :
$(RM) -r $(DESTDIR)$(docdir)

clean :
$(RM) *.o $(PROGRAMS) core
$(RM) *.a *.so *.o $(PROGRAMS) core
2 changes: 1 addition & 1 deletion dmioem.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <string.h>

#include "types.h"
#include "dmidecode.h"
#include "libdmi.h"
#include "dmioem.h"
#include "dmioutput.h"

Expand Down
2 changes: 1 addition & 1 deletion dmiopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "config.h"
#include "types.h"
#include "util.h"
#include "dmidecode.h"
#include "libdmi.h"
#include "dmiopt.h"


Expand Down
94 changes: 67 additions & 27 deletions dmioutput.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,97 +21,130 @@

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dmioutput.h"

const size_t output_size = 4096 * sizeof(char);
const size_t outout_threshold = 4096;
size_t next_realloc_ratio = 1;
char* output;

void pr_init()
{
output = malloc(output_size);
output[0] = '\0';
}
void pr_free()
{
free(output);
}

void output_realloc()
{
if ( (next_realloc_ratio * output_size) - strlen(output) < outout_threshold)
{
next_realloc_ratio++;
output = realloc(output, next_realloc_ratio * output_size);
}
}

void pr_comment(const char *format, ...)
{
output_realloc();
va_list args;

printf("# ");
sprintf(output + strlen(output), "# ");
va_start(args, format);
vprintf(format, args);
vsprintf(output + strlen(output), format, args);
va_end(args);
printf("\n");
sprintf(output + strlen(output), "\n");
}

void pr_info(const char *format, ...)
{
output_realloc();
va_list args;

va_start(args, format);
vprintf(format, args);
vsprintf(output + strlen(output), format, args);
va_end(args);
printf("\n");
sprintf(output + strlen(output), "\n");
}

void pr_handle(const struct dmi_header *h)
{
printf("Handle 0x%04X, DMI type %d, %d bytes\n",
h->handle, h->type, h->length);
output_realloc();
sprintf(output + strlen(output), "Handle 0x%04X, DMI type %d, %d bytes\n",
h->handle, h->type, h->length);
}

void pr_handle_name(const char *format, ...)
{
output_realloc();
va_list args;

va_start(args, format);
vprintf(format, args);
vsprintf(output + strlen(output), format, args);
va_end(args);
printf("\n");
sprintf(output + strlen(output), "\n");
}

void pr_attr(const char *name, const char *format, ...)
{
output_realloc();
va_list args;

printf("\t%s: ", name);
sprintf(output + strlen(output), "\t%s: ", name);

va_start(args, format);
vprintf(format, args);
vsprintf(output + strlen(output), format, args);
va_end(args);
printf("\n");
sprintf(output + strlen(output), "\n");
}

void pr_subattr(const char *name, const char *format, ...)
{
output_realloc();
va_list args;

printf("\t\t%s: ", name);
sprintf(output + strlen(output), "\t\t%s: ", name);

va_start(args, format);
vprintf(format, args);
vsprintf(output + strlen(output), format, args);
va_end(args);
printf("\n");
sprintf(output + strlen(output), "\n");
}

void pr_list_start(const char *name, const char *format, ...)
{
output_realloc();
va_list args;

printf("\t%s:", name);
sprintf(output + strlen(output), "\t%s:", name);

/* format is optional, skip value if not provided */
if (format)
{
printf(" ");
sprintf(output + strlen(output), " ");
va_start(args, format);
vprintf(format, args);
vsprintf(output + strlen(output), format, args);
va_end(args);
}
printf("\n");

sprintf(output + strlen(output), "\n");
}

void pr_list_item(const char *format, ...)
{
output_realloc();
va_list args;

printf("\t\t");
sprintf(output + strlen(output), "\t\t");

va_start(args, format);
vprintf(format, args);
vsprintf(output + strlen(output), format, args);
va_end(args);
printf("\n");
sprintf(output + strlen(output), "\n");
}

void pr_list_end(void)
Expand All @@ -121,17 +154,24 @@ void pr_list_end(void)

void pr_sep(void)
{
printf("\n");
output_realloc();
sprintf(output + strlen(output), "\n");
}

void pr_struct_err(const char *format, ...)
{
output_realloc();
va_list args;

printf("\t");
sprintf(output + strlen(output), "\t");

va_start(args, format);
vprintf(format, args);
vsprintf(output + strlen(output), format, args);
va_end(args);
printf("\n");
sprintf(output + strlen(output), "\n");
}

char* get_output()
{
return output;
}
5 changes: 4 additions & 1 deletion dmioutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include "dmidecode.h"
#include "libdmi.h"

void pr_init();
void pr_free();
void pr_comment(const char *format, ...);
void pr_info(const char *format, ...);
void pr_handle(const struct dmi_header *h);
Expand All @@ -32,3 +34,4 @@ void pr_list_item(const char *format, ...);
void pr_list_end(void);
void pr_sep(void);
void pr_struct_err(const char *format, ...);
char* get_output();
23 changes: 18 additions & 5 deletions dmidecode.c → libdmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
#include "config.h"
#include "types.h"
#include "util.h"
#include "dmidecode.h"
#include "libdmi.h"
#include "dmiopt.h"
#include "dmioem.h"
#include "dmioutput.h"
Expand Down Expand Up @@ -5563,8 +5563,9 @@ static int address_from_efi(off_t *address)
return ret;
}

int main(int argc, char * const argv[])
int procces(int argc, char * const argv[])
{
pr_init();
int ret = 0; /* Returned value */
int found = 0;
off_t fp;
Expand Down Expand Up @@ -5751,14 +5752,26 @@ int main(int argc, char * const argv[])
}
}
#endif

done:
if (!found && !(opt.flags & FLAG_QUIET))
pr_comment("No SMBIOS nor DMI entry point found, sorry.");

free(buf);
exit_free:
free(opt.type);

return ret;
}

char* decode_types(int argc, int* argv)
{
char* args[3] = {"dmidecode", "-t", ""};
char* type_ids = malloc(1000);
type_ids[0] = '\0';
for (int i=0; i < argc; i++)
{
sprintf(type_ids + strlen(type_ids), "%d,", argv[i]);
}
args[2] = type_ids;
procces(3,args);
return get_output();
}
Loading