summaryrefslogtreecommitdiff
path: root/plugins/task.c
blob: a6362e4c830820a80f2d5ba9e46589638c7bad2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <unistd.h>
#include <sys/wait.h>

#include <glib.h>

#include <connman/log.h>

#include "task.h"

struct task_data {
	pid_t pid;
	int index;
	task_cb_t callback;
	void *user_data;
};

static GSList *task_list = NULL;

struct task_data *task_find_by_pid(pid_t pid)
{
	GSList *list;

	for (list = task_list; list; list = list->next) {
		struct task_data *task = list->data;

		if (task->pid == pid)
			return task;
	}

	return NULL;
}

struct task_data *task_find_by_index(int index)
{
	GSList *list;

	for (list = task_list; list; list = list->next) {
		struct task_data *task = list->data;

		if (task->index == index)
			return task;
	}

	return NULL;
}

static void task_died(GPid pid, gint status, gpointer user_data)
{
	struct task_data *task = user_data;

	if (WIFEXITED(status))
		DBG("task %p exit status %d", task, WEXITSTATUS(status));
	else
		DBG("task %p signal %d", task, WTERMSIG(status));

	g_spawn_close_pid(pid);
	task->pid = 0;

	task_list = g_slist_remove(task_list, task);

	if (task->callback)
		task->callback(task->index, task->user_data);

	g_free(task);
}

static void task_setup(gpointer user_data)
{
	struct task_data *task = user_data;

	DBG("task %p", task);
}

struct task_data *task_spawn(int index, char **argv, char **envp,
					task_cb_t callback, void *user_data)
{
	GSpawnFlags flags = G_SPAWN_DO_NOT_REAP_CHILD |
						G_SPAWN_STDOUT_TO_DEV_NULL;
	struct task_data *task;

	DBG("index %d", index);

	task = g_try_new0(struct task_data, 1);
	if (task == NULL)
		return NULL;

	task->index = index;

	task->callback  = callback;
	task->user_data = user_data;

	if (g_spawn_async(NULL, argv, envp, flags,
				task_setup, task, &task->pid, NULL) == FALSE) {
		connman_error("Failed to spawn task");
		return NULL;
	}

	task_list = g_slist_append(task_list, task);

	g_child_watch_add(task->pid, task_died, task);

	DBG("task %p pid %d", task, task->pid);

	return task;
}

int task_kill(struct task_data *task)
{
	DBG("task %p", task);

	if (task->pid > 0)
		kill(task->pid, SIGTERM);

	return 0;
}

void *task_get_data(struct task_data *task)
{
	return task->user_data;
}