~ubuntu-branches/ubuntu/lucid/acpid/lucid-updates

« back to all changes in this revision

Viewing changes to ng/acpid/src/driver.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Meskes
  • Date: 2009-11-17 14:50:01 UTC
  • mfrom: (2.1.12 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091117145001-y5hevyg7gcg6uwjk
Tags: 1.0.10-4
Updated netlink patch to version 6.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <string.h>
 
3
#include <sys/types.h>
 
4
#include <unistd.h>
 
5
#include <unistd.h>
 
6
#include <stdio.h>
 
7
#include <stdlib.h>
 
8
#include <acpid/driver.h>
 
9
#include <sys/wait.h>
 
10
 
 
11
int acpi_channel_create(lua_State *L)
 
12
{
 
13
        lua_pushinteger(L, 1);
 
14
        lua_gettable(L, -3);
 
15
 
 
16
        const char *name = lua_tostring(L, -1);
 
17
        const struct acpi_driver_ops *ops = acpi_driver_find(name);
 
18
        lua_pop(L, 1);
 
19
 
 
20
        if (ops == NULL)
 
21
                return -1;
 
22
 
 
23
        lua_pushinteger(L, 2);
 
24
        lua_gettable(L, -3);
 
25
 
 
26
        struct acpi_channel *channel = malloc(sizeof(struct acpi_channel));
 
27
        int ret = (*ops->create)(channel, L);
 
28
        lua_pop(L, 1);
 
29
 
 
30
        if (ret)
 
31
                return -1;
 
32
 
 
33
        lua_pushlightuserdata(L, channel);
 
34
 
 
35
        return 0;
 
36
}
 
37
 
 
38
void acpi_channel_register(struct acpi_channel *channel, const struct acpi_channel_ops *ops)
 
39
{
 
40
        channel->ops = ops;
 
41
}
 
42
 
 
43
void acpi_channel_watch(struct acpi_channel *channel, struct acpi_channel_descriptor *cds, int fd, int events)
 
44
{
 
45
        cds->channel = channel;
 
46
 
 
47
        cds->fd = fd;
 
48
        cds->events = events;
 
49
}
 
50
 
 
51
static int printL(lua_State *L)
 
52
{
 
53
        printf(">>> ");
 
54
        for (int i = 1; i < lua_gettop(L); ++i)
 
55
                printf("%s, ", lua_tostring(L, i));
 
56
        printf("%s\n", lua_tostring(L, lua_gettop(L)));
 
57
 
 
58
        return 0;
 
59
}
 
60
 
 
61
static int execL(lua_State *L)
 
62
{
 
63
        if (lua_gettop(L) > 12)
 
64
                return 0;
 
65
 
 
66
        const char *args[12] = { lua_tostring(L, 1) };
 
67
        for (int i = 1; i < 12; ++i)
 
68
                args[i] = lua_tostring(L, i + 1);
 
69
 
 
70
        pid_t pid = fork();
 
71
        switch (pid) {
 
72
        case -1:
 
73
                exit(1);
 
74
        case 0:
 
75
                execvp(lua_tostring(L, 1), args);
 
76
                break;
 
77
        default:
 
78
                waitpid(pid, NULL, 0);
 
79
        }
 
80
 
 
81
        return 0;
 
82
}
 
83
 
 
84
int acpi_channel_event(struct acpi_channel *channel, lua_State *L, const char *hid, unsigned long event, unsigned long data)
 
85
{
 
86
        /* Send a dbus event */
 
87
        acpi_dbus_event(hid, event, data);
 
88
 
 
89
        /* Setup en basic Lua environment */
 
90
        lua_pushcclosure(L, execL, 0);
 
91
        lua_setfield(L, LUA_GLOBALSINDEX, "exec");
 
92
 
 
93
        lua_pushcclosure(L, printL, 0);
 
94
        lua_setfield(L, LUA_GLOBALSINDEX, "print");
 
95
 
 
96
        /* First we let the channel handler process the event. */
 
97
        lua_pushlightuserdata(L, channel);
 
98
        lua_gettable(L, LUA_REGISTRYINDEX);
 
99
 
 
100
        lua_pushstring(L, hid);
 
101
        lua_pushinteger(L, event);
 
102
        lua_pushinteger(L, data);
 
103
 
 
104
        /* TODO: For now we ignore the return values. */
 
105
        lua_pcall(L, 3, 0, 0);
 
106
 
 
107
        /* Now we pass the arguments to all script functions. If a function     
 
108
         * returns something else than nil, we abort the processing. */
 
109
        lua_getfield(L, LUA_GLOBALSINDEX, "script");
 
110
        for (int i = 1; i < 100; ++i) {
 
111
                lua_pushinteger(L, i);
 
112
                lua_gettable(L, -2);
 
113
 
 
114
                if (lua_type(L, -1) == LUA_TFUNCTION) {
 
115
                        lua_pushstring(L, hid);
 
116
                        lua_pushinteger(L, event);
 
117
                        lua_pushinteger(L, data);
 
118
 
 
119
                        if (lua_pcall(L, 3, 1, 0) || lua_type(L, -1)) {
 
120
                                printf("script[%02d] processing aborted: %s\n", i, lua_tostring(L, -1));
 
121
                                lua_pop(L, 1);
 
122
                                break;
 
123
                        }
 
124
                }
 
125
 
 
126
                lua_pop(L, 1);
 
127
        }
 
128
}
 
129
 
 
130
 
 
131
 
 
132
/*
 
133
 *      ACPI Driver
 
134
 */
 
135
 
 
136
static const struct acpi_driver *drivers[4];
 
137
static unsigned char num;
 
138
 
 
139
 
 
140
void acpi_driver_register(const struct acpi_driver *driver)
 
141
{
 
142
        drivers[num++] = driver;
 
143
}
 
144
 
 
145
const struct acpi_driver_ops *acpi_driver_find(const char *name)
 
146
{
 
147
        for (int i = 0; i < num; ++i) {
 
148
                if (strcmp(drivers[i]->name, name) == 0)
 
149
                        return &drivers[i]->ops;
 
150
        }
 
151
 
 
152
        return NULL;
 
153
}
 
154