Skip to content

Commit 856d9f0

Browse files
committed
lacpi_utils: propogate errors
1 parent 3c8ae7c commit 856d9f0

File tree

7 files changed

+200
-92
lines changed

7 files changed

+200
-92
lines changed

stand/liblua/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ SRCS+= lhash.c
3232
# ACPI
3333
.if ${MACHINE_CPUARCH} == "amd64" && ${DO32:U0} == 0
3434
.PATH: ${LIBLUASRC}/acpi
35-
SRCS+= lacpi.c lacpi_object.c lacpi_utils.c lacpi_walk.c lacp_data.c
35+
SRCS+= lacpi.c lacpi_object.c lacpi_utils.c lacpi_walk.c lacpi_data.c
3636
CFLAGS+= -I${SYSDIR}/contrib/dev/acpica/include \
3737
-I${EFISRC}/libacpi/acpi/include -I${EFISRC}/include \
3838
-I${EFISRC}/include/amd64 -I${LIBLUASRC}/acpi/include

stand/liblua/acpi/include/lacpi_utils.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
/***** UTILITY *****/
99

1010
/* verifies lua passed over a handle on the stack */
11-
ACPI_HANDLE lua_check_handle(lua_State *L, int idx);
11+
ACPI_HANDLE lacpi_check_handle(lua_State *L, int idx);
1212

1313
/* destructor dispatcher */
1414
typedef void (*acpi_destructor_t)(ACPI_OBJECT *);
1515

1616
/* safety check -- lua stores integers as 64bit */
17-
UINT32 lua_int_to_uint32(lua_State *L, int index, const char *errmsg);
17+
ACPI_STATUS lacpi_int_to_uint32(lua_State *L, int index, UINT32 *num);
18+
19+
/* build error code and push it onto stack */
20+
int lacpi_push_err(lua_State *L, const int push_nil, const char *errmsg,
21+
const ACPI_STATUS status);
22+
23+
/* extract error message relating to ACPI_STATUS */
24+
char *lacpi_extract_status(const ACPI_STATUS status);
1825

1926
/***** FACTORY *****/
2027

@@ -23,14 +30,14 @@ int lacpi_create_mt_gc(lua_State *L, const char *mt, lua_CFunction gc_func);
2330

2431
/*** ACPI_OBJECT ***/
2532
/* build ACPI_OBJECTs */
26-
void build_int(lua_State *L, ACPI_OBJECT *obj);
27-
void build_str(lua_State *L, ACPI_OBJECT *obj);
28-
void build_buff(lua_State *L, ACPI_OBJECT *obj);
29-
void build_pkg(lua_State *L, ACPI_OBJECT *obj);
30-
void build_acpi_obj(lua_State *L, ACPI_OBJECT *obj, UINT32 obj_type);
31-
int build_ref(lua_State *L, ACPI_OBJECT *obj);
32-
int build_proc(lua_State *L, ACPI_OBJECT *obj);
33-
int build_pow(lua_State *L, ACPI_OBJECT *obj);
33+
ACPI_STATUS build_int(lua_State *L, ACPI_OBJECT *obj);
34+
ACPI_STATUS build_str(lua_State *L, ACPI_OBJECT *obj);
35+
ACPI_STATUS build_buff(lua_State *L, ACPI_OBJECT *obj);
36+
ACPI_STATUS build_pkg(lua_State *L, ACPI_OBJECT *obj);
37+
ACPI_STATUS build_acpi_obj(lua_State *L, ACPI_OBJECT *obj, UINT32 obj_type);
38+
ACPI_STATUS build_ref(lua_State *L, ACPI_OBJECT *obj);
39+
ACPI_STATUS build_proc(lua_State *L, ACPI_OBJECT *obj);
40+
ACPI_STATUS build_pow(lua_State *L, ACPI_OBJECT *obj);
3441

3542
/* push ACPI_OBJECTs onto lua stack */
3643
void push_int(lua_State *L, ACPI_OBJECT *obj);

stand/liblua/acpi/lacpi_data.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,19 @@ lAcpiAttachData(lua_State *L)
6969
{
7070
ACPI_STATUS status;
7171
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
72-
UINT32 type = lua_int_to_uint32(L, 2, "Object type must be 32 bit");
72+
UINT32 type;
7373
ACPI_OBJECT *obj;
7474

7575
if (handle == NULL) {
7676
return luaL_error(L, "lAcpiAttachData: Handle is NULL");
7777
}
7878

79+
if (ACPI_FAILURE(status = lacpi_int_to_uint32(L, 2, &type))) {
80+
lua_pushnil(L);
81+
lua_pushstring(L, "ACPI_OBJECT_TYPE must be a UINT32");
82+
return 2;
83+
}
84+
7985
obj = malloc(sizeof(ACPI_OBJECT));
8086
if (obj == NULL) {
8187
return luaL_error(L,

stand/liblua/acpi/lacpi_object.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ lAcpiGetHandle(lua_State *L)
1818
ACPI_STATUS status;
1919

2020
if (ACPI_FAILURE(status = AcpiGetHandle(NULL, pathname, &handle))) {
21-
return luaL_error(L, "AcpiGetHandle failed with status 0x%x", status);
21+
return lacpi_push_err(L, 1,
22+
"AcpiGetHandleFailed with status: ", status);
2223
}
2324

2425
lua_pushlightuserdata(L, handle);
@@ -43,6 +44,7 @@ static int
4344
lAcpiEvaluateObject(lua_State *L)
4445
{
4546
ACPI_STATUS status;
47+
char errmsg[128];
4648
ACPI_OBJECT_LIST obj_list;
4749
ACPI_HANDLE handle = NULL;
4850
ACPI_OBJECT *objs = NULL;
@@ -70,7 +72,7 @@ lAcpiEvaluateObject(lua_State *L)
7072
} else if (abs_path != NULL) {
7173
pathname = strdup(abs_path);
7274
} else {
73-
return luaL_error(L, "Incorrect arguments");
75+
return lacpi_push_err(L, 1, "Incorrect arguments", 0);
7476
}
7577

7678
/*
@@ -98,17 +100,20 @@ lAcpiEvaluateObject(lua_State *L)
98100
lua_rawgeti(L, 4, i + 1);
99101
lua_getfield(L, -1, "obj_type");
100102

101-
if ((status = lua_int_to_uint32(L, -1, &obj_type))
102-
!= AE_OK) {
103-
lua_pushnil(L);
104-
lua_pushstring(L, "ACPI_OBJECT_TYPE must be"
105-
" UINT32");
103+
if (ACPI_FAILURE(status = lacpi_int_to_uint32(L, -1,
104+
&obj_type))) {
106105
free(objs);
107-
return 2;
106+
return lacpi_push_err(L, 1,
107+
"ACPI_OBJECT_TYPE must be UINT32", 0);
108108
}
109109
lua_pop(L, 1);
110110

111-
build_acpi_obj(L, &objs[i], obj_type);
111+
if (ACPI_FAILURE(status =
112+
build_acpi_obj(L, &objs[i], obj_type))) {
113+
snprintf(errmsg, sizeof(errmsg),
114+
"Failed to build objs[%d]", i);
115+
return lacpi_push_err(L, 1, errmsg, status);
116+
}
112117
lua_pop(L, 1);
113118
}
114119

@@ -128,11 +133,8 @@ lAcpiEvaluateObject(lua_State *L)
128133
free_acpi_objs(objs, obj_count);
129134

130135
char errbuf[64];
131-
snprintf(errbuf, sizeof(errbuf),
132-
"AcpiEvaluateObject failed with status 0x%x", status);
133-
lua_pushnil(L);
134-
lua_pushstring(L, errbuf);
135-
return 2;
136+
return lacpi_push_err(L, 1,
137+
"AcpiEvaluateObject failed with status", status);
136138
}
137139

138140
if (return_buffer.Pointer != NULL) {

0 commit comments

Comments
 (0)