process.h File Reference

a simple object-oriented process manager object model inspired by Zed Shaw More...

#include <stdlib.h>
#include <stdbool.h>
#include "util.h"
#include "obj.h"

Go to the source code of this file.

Classes

struct  co_process_t
 

Typedefs

typedef struct co_process_t co_process_t
 

Enumerations

enum  co_process_state_t {
  _STOPPING = 0, _STOPPED =1, _STARTING = 2, _STARTED = 3,
  _FAILED = -1
}
 

Functions

struct co_process_t __attribute__ ((packed))
 
co_obj_tco_process_create (size_t size, co_process_t proto, const char *name, const char *pid_file, const char *exec_path, const char *run_path)
 creates a new commotion process More...
 
int co_process_destroy (co_obj_t *self)
 removes a process from commotiond More...
 
int co_process_start (co_obj_t *self, char *argv[])
 starts a selected process More...
 
int co_process_stop (co_obj_t *self)
 stops a running process More...
 
int co_process_restart (co_obj_t *self)
 restarts a process More...
 

Variables

typedef __attribute__
 
co_obj_t _header
 
uint8_t _exttype
 
uint8_t _len
 
int pid
 
bool registered
 
bool use_watchdog
 
co_process_state_t state
 
char * name
 
char * pid_file
 
char * exec_path
 
char * run_path
 
int input
 
int output
 
int(* init )(co_obj_t *self)
 
int(* destroy )(co_obj_t *self)
 
int(* start )(co_obj_t *self, char *argv[])
 
int(* stop )(co_obj_t *self)
 
int(* restart )(co_obj_t *self)
 

Detailed Description

a simple object-oriented process manager object model inspired by Zed Shaw

Author
Josh King (jheretic), jking.nosp@m.@cha.nosp@m.mbana.nosp@m..net

Enumeration Type Documentation

defines the state of the process

45  {
46  _STOPPING = 0,
47  _STOPPED =1,
48  _STARTING = 2,
49  _STARTED = 3,
50  _FAILED = -1
co_process_state_t
Definition: process.h:45

Function Documentation

co_obj_t* co_process_create ( size_t  size,
co_process_t  proto,
const char *  name,
const char *  pid_file,
const char *  exec_path,
const char *  run_path 
)

creates a new commotion process

Parameters
sizesize of the process
co_process_tprotocol
namename of the process
pid_filethe lockfile where the process id is stored
exec_paththe execution path
run_paththe run path
Returns
co_process_t to be registered with the daemon

References co_process_destroy(), co_process_start(), and co_process_stop().

44  {
45  if(!proto.init) proto.init = NULL;
46  if(!proto.destroy) proto.destroy = co_process_destroy;
47  if(!proto.start) proto.start = co_process_start;
48  if(!proto.stop) proto.stop = co_process_stop;
49 
50  co_process_t *new_proc = h_calloc(1,size);
51  *new_proc = proto;
52  new_proc->_header._type = _ext8;
53  new_proc->_exttype = _process;
54  new_proc->_len = size;
55 
56  CHECK_MEM(new_proc);
57 
58  new_proc->name = h_strdup(name);
59  hattach(new_proc->name,new_proc);
60  new_proc->pid_file = h_strdup(pid_file);
61  hattach(new_proc->pid_file,new_proc);
62  new_proc->exec_path = h_strdup(exec_path);
63  hattach(new_proc->exec_path,new_proc);
64  new_proc->run_path = h_strdup(run_path);
65  hattach(new_proc->run_path,new_proc);
66 
67  if(!new_proc->init((co_obj_t*)new_proc)) {
68  SENTINEL("Failed to initialize new process.");
69  } else {
70  return (co_obj_t*)new_proc;
71  }
72 
73 error:
74  new_proc->destroy((co_obj_t*)new_proc);
75  return NULL;
76 }
int co_process_destroy(co_obj_t *self)
removes a process from commotiond
Definition: process.c:78
int co_process_stop(co_obj_t *self)
stops a running process
Definition: process.c:136
Definition: obj.h:131
Definition: process.h:58
co_obj_t * name
Definition: cmd.h:60
int co_process_start(co_obj_t *self, char *argv[])
starts a selected process
Definition: process.c:91
int co_process_destroy ( co_obj_t self)

removes a process from commotiond

Parameters
selfpointer to the process' struct

Referenced by co_process_create().

78  {
79  CHECK_MEM(self);
80  CHECK(IS_PROCESS(self),"Not a process.");
81 
82  co_obj_free(self);
83 
84  return 1;
85 
86 error:
87  return 0;
88 }
int co_process_restart ( co_obj_t self)

restarts a process

Parameters
selfpointer to the process' struct
148  {
149  CHECK_MEM(self);
150  CHECK(IS_PROCESS(self),"Not a process.");
151  co_process_t *this = (co_process_t*)self;
152  if(this->stop((co_obj_t*)this)) this->start((co_obj_t*)this, NULL);
153 
154  return 1;
155 
156 error:
157  return 0;
158 }
Definition: obj.h:131
Definition: process.h:58
int co_process_start ( co_obj_t self,
char *  argv[] 
)

starts a selected process

Parameters
selfpointer to the process' struct
argv[]execution path for the process

References exec.

Referenced by co_process_create().

91  {
92  CHECK_MEM(self);
93  CHECK(IS_PROCESS(self),"Not a process.");
94  co_process_t *this = (co_process_t*)self;
95  char *exec = NULL;
96  CHECK(((exec = this->exec_path) != NULL), "Invalid exec path!");
97  int pid = 0;
98  int local_stdin_pipe[2], local_stdout_pipe[2];
99 
100  if(pipe(local_stdin_pipe)) {
101  ERROR("Could not initialize stdin pipe.");
102  return 0;
103  }
104  if(pipe(local_stdout_pipe)) {
105  ERROR("Could not initialize stdout pipe.");
106  return 0;
107  }
108 
109  if (!(pid = fork())) {
110  dup2(local_stdin_pipe[0], 0);
111  dup2(local_stdout_pipe[1], 1);
112 
113  close(local_stdin_pipe[0]);
114  close(local_stdin_pipe[1]);
115  close(local_stdout_pipe[0]);
116  close(local_stdout_pipe[1]);
117 
118  CHECK((execvp(exec, argv) != -1), "Failed to exec!");
119  }
120  INFO("fork()ed: %d\n", pid);
121  this->pid = pid;
122 
123  this->input = local_stdin_pipe[1];
124  this->output = local_stdout_pipe[0];
125 
126  close(local_stdin_pipe[0]);
127  close(local_stdout_pipe[1]);
128 
129  return 1;
130 
131 error:
132  exit(1);
133  return 0;
134 }
co_cb_t exec
Definition: cmd.h:59
Definition: process.h:58
int co_process_stop ( co_obj_t self)

stops a running process

Parameters
selfpointer to the process' struct

Referenced by co_process_create().

136  {
137  CHECK_MEM(self);
138  CHECK(IS_PROCESS(self),"Not a process.");
139  co_process_t *this = (co_process_t*)self;
140  CHECK(!(kill(this->pid, SIGKILL)), "Failed to kill co_process_t %d.", this->pid);
141 
142  return 1;
143 
144 error:
145  return 0;
146 }
Definition: process.h:58