Flesh out Service struct
This commit is contained in:
parent
96e962a08c
commit
ecc818534c
22
daemon.h
22
daemon.h
@ -19,6 +19,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
typedef unsigned char byte;
|
typedef unsigned char byte;
|
||||||
@ -63,10 +64,29 @@ extern uid_t serviceUID;
|
|||||||
extern gid_t serviceGID;
|
extern gid_t serviceGID;
|
||||||
extern char *serviceEnviron[EnvironLen];
|
extern char *serviceEnviron[EnvironLen];
|
||||||
|
|
||||||
|
enum State {
|
||||||
|
Stop,
|
||||||
|
Start,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum { LineCap = 512 };
|
||||||
|
struct Line {
|
||||||
|
size_t len;
|
||||||
|
char buf[LineCap];
|
||||||
|
};
|
||||||
|
|
||||||
struct Service {
|
struct Service {
|
||||||
char *name;
|
char *name;
|
||||||
char *command;
|
char *command;
|
||||||
// TODO: And many other things...
|
enum State intent;
|
||||||
|
enum State state;
|
||||||
|
pid_t pid;
|
||||||
|
int outPipe[2];
|
||||||
|
int errPipe[2];
|
||||||
|
struct Line outLine;
|
||||||
|
struct Line errLine;
|
||||||
|
int restartInterval;
|
||||||
|
struct timeval restartTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct Services {
|
extern struct Services {
|
||||||
|
24
service.c
24
service.c
@ -14,6 +14,7 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -36,6 +37,14 @@ struct Services services;
|
|||||||
static void serviceFree(struct Service *service) {
|
static void serviceFree(struct Service *service) {
|
||||||
free(service->name);
|
free(service->name);
|
||||||
free(service->command);
|
free(service->command);
|
||||||
|
if (service->outPipe[0] >= 0) {
|
||||||
|
close(service->outPipe[0]);
|
||||||
|
close(service->outPipe[1]);
|
||||||
|
}
|
||||||
|
if (service->errPipe[0] >= 0) {
|
||||||
|
close(service->errPipe[0]);
|
||||||
|
close(service->errPipe[1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int serviceAdd(const char *name, const char *command) {
|
int serviceAdd(const char *name, const char *command) {
|
||||||
@ -63,7 +72,11 @@ int serviceAdd(const char *name, const char *command) {
|
|||||||
services.ptr = ptr;
|
services.ptr = ptr;
|
||||||
}
|
}
|
||||||
service = &services.ptr[services.len];
|
service = &services.ptr[services.len];
|
||||||
memset(service, 0, sizeof(*service));
|
*service = (struct Service) {
|
||||||
|
.outPipe = { -1, -1 },
|
||||||
|
.errPipe = { -1, -1 },
|
||||||
|
.restartInterval = restartInterval,
|
||||||
|
};
|
||||||
|
|
||||||
service->name = strdup(name);
|
service->name = strdup(name);
|
||||||
if (!service->name) goto err;
|
if (!service->name) goto err;
|
||||||
@ -71,6 +84,15 @@ int serviceAdd(const char *name, const char *command) {
|
|||||||
service->command = strdup(command);
|
service->command = strdup(command);
|
||||||
if (!service->command) goto err;
|
if (!service->command) goto err;
|
||||||
|
|
||||||
|
int error = pipe2(service->outPipe, O_CLOEXEC);
|
||||||
|
if (error) goto err;
|
||||||
|
|
||||||
|
error = pipe2(service->errPipe, O_CLOEXEC);
|
||||||
|
if (error) goto err;
|
||||||
|
|
||||||
|
fcntl(service->outPipe[0], F_SETFL, O_NONBLOCK);
|
||||||
|
fcntl(service->errPipe[0], F_SETFL, O_NONBLOCK);
|
||||||
|
|
||||||
services.len++;
|
services.len++;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user