Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / simulator / simulator / core / robot.c @ 988

History | View | Annotate | Download (3.32 KB)

1
/**
2
 * @file robot.c
3
 * @author Colony Project
4
 * @brief Simulator Robot Management
5
 *
6
 * Contains implementations of functions managing robots
7
 * in the simulator, including process control and memory
8
 * management.
9
 **/
10

    
11
#include <stdlib.h>
12
#include <stdio.h>
13
#include <string.h>
14

    
15
#include <unistd.h>
16
#include <signal.h>
17
#include <sys/wait.h>
18
#include <robot_shared.h>
19
#include <sys/ipc.h>
20
#include <sys/types.h>
21
#include <sys/shm.h>
22
#include <errno.h>
23

    
24
#include "robot.h"
25

    
26
void sig_chld_handler(int sig);
27

    
28
// global variables
29
int first_available_id = 0;
30
// an expanding array of robots
31
Robot* robots = NULL;
32
int robots_size = 0;
33

    
34
int robots_initialize(void)
35
{
36
        if (signal(SIGCHLD, sig_chld_handler) == SIG_ERR)
37
                return -1;
38

    
39
        robots_size = 10;
40
        robots = (Robot*)malloc(sizeof(Robot) * robots_size);
41
        if (!robots)
42
                return -1;
43
        memset(robots, 0, sizeof(Robot) * robots_size);
44

    
45
        return 0;
46
}
47

    
48
/**
49
 * Creates a new robot. Returns its id on
50
 * success, or a negative integer on failure.
51
 **/
52
int robot_create(char *execname)
53
{
54
        int pid;
55
        int id = first_available_id;
56
        Robot* r = &robots[id];
57
        // do shared memory stuff here
58
        key_t key = IPC_PRIVATE;
59
        //Memory accessible only to children with r/w privileges
60
        r->sharedMemID = shmget(key, sizeof(RobotShared), IPC_CREAT | 0666);
61
        
62
        if(r->sharedMemID < 0)
63
        {
64
                fprintf(stderr, "Failed to get shared memory.\n");
65
                return -1;
66
        }
67

    
68
        r->shared = (RobotShared*)shmat(r->sharedMemID, NULL, 0);
69
        
70
        if(!(r->shared))
71
        {
72
                // TODO: do we have to free the shared memory?
73
                fprintf(stderr, "Error attaching memory to parent.\n");
74
                return -1;
75
        }
76
        
77
        // Initialize robot structure here
78
        r->shared->motor1 = 0;
79
        r->shared->motor2 = 0;
80
        r->id = id;
81

    
82
        if((pid = fork()) < 0)
83
        {
84
                // TODO: do we have to free the shared memory?
85
                r->id = 0;
86
                fprintf(stderr, "Failed to fork robot process.\n");
87
                return -1;
88
        }                                                                                         
89
        
90

    
91
        if(!pid)
92
        {
93
                char var[21];
94
                char *envp[] = {var, NULL};
95
                /* restore default sigchld handler */
96
                signal(SIGCHLD, SIG_DFL);
97
                sprintf(var, "memory_id=%d", r->sharedMemID);
98
                //TODO: keep the other env. stuff around
99
                execve(execname, NULL, envp);
100
                fprintf(stderr, "exec failed to run child process.\n");
101
                exit(-1);
102
        }
103
        else
104
        {
105
                r->pid = pid;
106
                do {
107
                        first_available_id++;
108
                } while (first_available_id < robots_size &&
109
                                robots[first_available_id].id != 0);
110
                // resize the array if necessary
111
                if (first_available_id >= robots_size)
112
                {
113
                        robots_size *= 2;
114
                        robots = realloc(robots, sizeof(Robot) * robots_size);
115
                        if (!robots)
116
                        {
117
                                fprintf(stderr, "Out of memory.\n");
118
                                return -1;
119
                        }
120
                }
121
        }
122
        
123
        return id;
124
}
125

    
126
/**
127
 * Frees all resources associated with a robot, including
128
 * killing its process if necessary.
129
 *
130
 * @return zero on success, nonzero on failure
131
 **/
132
int robot_destroy(int id)
133
{
134
        Robot* r;
135

    
136
        if (id < 0 || id >= robots_size)
137
                return -1;
138
        r = &robots[id];
139
        if (r->id == 0)
140
                return -1;
141

    
142
        if (!shmdt(r->shared))
143
        {
144
                fprintf(stderr, "Failed to free shared memory.\n");
145
                return -1;
146
        }
147
        if (!shmctl(r->sharedMemID, IPC_RMID, NULL))
148
        {
149
                fprintf(stderr, "Failed to free shared memory.\n");
150
                return -1;
151
        }
152

    
153
        memset(r, 0, sizeof(Robot));
154

    
155
        if (id < first_available_id)
156
                first_available_id = 0;
157
        
158
        return 0;
159
}
160

    
161
void sig_chld_handler(int sig)
162
{
163
        pid_t pid;
164
        int cstat;
165

    
166
        printf("SIGCHLD: parent waiting\n");
167

    
168
        pid = waitpid(0, &cstat, WUNTRACED);
169

    
170
        printf("got %d from %d\n", cstat, pid);
171

    
172
        kill(pid, SIGCONT);
173
}
174