source: roaraudio/roard/roard.c @ 444:ddaaf8e220bd

Last change on this file since 444:ddaaf8e220bd was 444:ddaaf8e220bd, checked in by phi, 16 years ago

added support vor setuid, setgid and chroot

File size: 10.5 KB
Line 
1//roard.c:
2
3#include "roard.h"
4
5char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address
6
7void usage (void) {
8 printf("Usage: roard [OPTIONS]...\n\n");
9
10 printf("Misc Options:\n\n");
11 printf(
12        " --demon               - Bring the server into background after init\n"
13        " --terminate           - Terminate after last client quited\n"
14        " --restart             - Trys to stop an old instance and start a new with new settings\n"
15        " --realtime            - Trys to get realtime priority,\n"
16        "                         give multible times for being more realtime\n"
17        " --chroot DIR          - chroots to the given dir\n"
18        " --setgid              - GroupID to the audio group as specified via -G\n"
19        " --setuid              - UserID to the audio user as specified via -U\n"
20       );
21
22 printf("\nAudio Options:\n\n");
23 printf(
24        " -R  --rate   RATE     - Set server rate\n"
25        " -B  --bits   BITS     - Set server bits\n"
26        " -C  --chans  CHANNELS - Set server channels\n"
27       );
28
29 printf("\nDriver Options:\n\n");
30 printf(" -d  --driver DRV      - Set the driver, use '-d list' to get a list (default: %s)\n", ROAR_DRIVER_DEFAULT);
31 printf(" -D  --device DEV      - Set the device\n");
32 printf(" -dO OPTS              - Set output options\n");
33
34 printf("\nSource Options:\n\n");
35 printf(" -s  --source DRV      - Use DRV as input driver\n"
36        " -S           DEV      - Use DEV as input device\n"
37        " -sO          OPTS     - Use OPTS as input options\n"
38        " -sP                   - Make souce as primary\n"
39       );
40
41 printf("\nCodec Filter Options:\n\n");
42 printf(" --list-cf             - List all codec filter\n"
43       );
44
45 printf("\nServer Options:\n\n");
46 printf(" -t  --tcp             - Use TCP listen socket\n"
47        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
48        " -p  --port            - TCP Port to bind to\n"
49        " -b  --bind            - IP/Hostname to bind to\n"
50        " -s  --sock            - Filename for UNIX Domain Socket\n"
51        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: audio)\n"
52        "                         You need the permittions to change the GID\n"
53        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
54        "                         You need the permittions to change the UID (normaly only root has)\n"
55        " --no-listen           - Do not listen for new clients (only usefull for relaing)\n"
56        " --client-fh           - Comunicate with a client over this handle\n"
57        "                         (only usefull for relaing)\n"
58       );
59// printf("\n Options:\n\n");
60 printf("\n");
61}
62
63#define R_SETUID 1
64#define R_SETGID 2
65
66int main (int argc, char * argv[]) {
67 int i;
68 char * k;
69 char user_sock[80] = {0};
70 struct roar_audio_info sa;
71 int    demon    = 0;
72 int    realtime = 0;
73 char * driver = getenv("ROAR_DRIVER");
74 char * device = getenv("ROAR_DEVICE");
75 char * opts   = NULL;
76// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
77 int      port = ROAR_DEFAULT_PORT;
78 int               drvid;
79 char * s_dev     = NULL;
80 char * s_con     = NULL;
81 char * s_opt     = NULL;
82 int    s_prim    = 0;
83 char * sock_grp  = "audio";
84 char * sock_user = NULL;
85 char * chrootdir = NULL;
86 int    setids    = 0;
87 struct group  * grp = NULL;
88 struct passwd * pwd = NULL;
89 DRIVER_USERDATA_T drvinst;
90 struct roar_client * self = NULL;
91
92 g_listen_socket = -1;
93 g_standby       =  0;
94
95 sa.bits     = ROAR_BITS_DEFAULT;
96 sa.channels = ROAR_CHANNELS_DEFAULT;
97 sa.rate     = ROAR_RATE_DEFAULT;
98 sa.codec    = ROAR_CODEC_DEFAULT;
99
100 g_sa = &sa;
101
102
103 if ( getuid() != 0 && getenv("HOME") ) {
104  snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
105  server = user_sock;
106 }
107
108 if ( getenv("ROAR_SERVER") != NULL )
109  server = getenv("ROAR_SERVER");
110
111 if ( clients_init() == -1 ) {
112  ROAR_ERR("Can not init clients!");
113  return 1;
114 }
115
116 if ( streams_init() == -1 ) {
117  ROAR_ERR("Can not init streams!");
118  return 1;
119 }
120
121 if ( (g_self_client = clients_new()) == -1 ) {
122  ROAR_ERR("Can not create self client!");
123  return 1;
124 }
125
126 if ( sources_init() == -1 ) {
127  ROAR_ERR("Can not init sources!");
128  return 1;
129 }
130
131 if ( (sources_set_client(g_self_client)) == -1 ) {
132  ROAR_ERR("Can not init set source client!");
133  return 1;
134 }
135
136 for (i = 1; i < argc; i++) {
137  k = argv[i];
138
139  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
140   usage();
141   return 0;
142
143  } else if ( strcmp(k, "--demon") == 0 ) {
144   demon = 1;
145  } else if ( strcmp(k, "--terminate") == 0 ) {
146   g_terminate = 1;
147  } else if ( strcmp(k, "--realtime") == 0 ) {
148   realtime++;
149  } else if ( strcmp(k, "--chroot") == 0 ) {
150   chrootdir = argv[++i];
151  } else if ( strcmp(k, "--setgid") == 0 ) {
152   setids |= R_SETGID;
153  } else if ( strcmp(k, "--setuid") == 0 ) {
154   setids |= R_SETUID;
155
156  } else if ( strcmp(k, "--list-cf") == 0 ) {
157   print_codecfilterlist();
158   return 0;
159
160  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
161   sa.rate = atoi(argv[++i]);
162  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
163   sa.bits = atoi(argv[++i]);
164  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
165   sa.channels = atoi(argv[++i]);
166
167  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
168   driver = argv[++i];
169   if ( strcmp(driver, "list") == 0 ) {
170    print_driverlist();
171   }
172  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
173   device = argv[++i];
174  } else if ( strcmp(k, "-dO") == 0 ) {
175   opts = argv[++i];
176
177  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
178   k = argv[++i];
179   if ( sources_add(k, s_dev, s_con, s_opt, s_prim) == -1 ) {
180    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, k);
181   }
182   s_opt = s_dev = s_con = NULL;
183   s_prim = 0;
184  } else if ( strcmp(k, "-S") == 0 ) {
185   s_dev = argv[++i];
186  } else if ( strcmp(k, "-sO") == 0 ) {
187   s_opt = argv[++i];
188  } else if ( strcmp(k, "-sC") == 0 ) {
189   s_con = argv[++i];
190  } else if ( strcmp(k, "-sP") == 0 ) {
191   s_prim = 1;
192
193  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
194   port = atoi(argv[++i]);
195  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "-s") == 0 || strcmp(k, "--sock") == 0 ) {
196   server = argv[++i];
197  } else if ( strcmp(k, "-t") == 0 ) {
198   server = ROAR_DEFAULT_HOST;
199  } else if ( strcmp(k, "-u") == 0 ) {
200   // ignore this case as it is the default behavor.
201  } else if ( strcmp(k, "-G") == 0 ) {
202   sock_grp  = argv[++i];
203  } else if ( strcmp(k, "-U") == 0 ) {
204   sock_user = argv[++i];
205
206  } else if ( strcmp(k, "--no-listen") == 0 ) {
207   *server = 0;
208  } else if ( strcmp(k, "--client-fh") == 0 ) {
209   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
210    ROAR_ERR("main(*): Can not set client's fh");
211    return 1;
212   }
213
214  } else {
215   usage();
216   return 1;
217  }
218
219 }
220
221 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
222
223 if ( midi_init() == -1 )
224  ROAR_ERR("Can not initialize MIDI subsystem");
225
226 if ( *server != 0 ) {
227  if ( (g_listen_socket = roar_socket_listen(ROAR_SOCKET_TYPE_UNKNOWN, server, port)) == -1 ) {
228   if ( *server == '/' ) {
229    if ( (i = roar_socket_connect(server, port)) != -1 ) {
230     close(i);
231     ROAR_ERR("Can not open listen socket!");
232     return 1;
233    } else {
234     unlink(server);
235     if ( (g_listen_socket = roar_socket_listen(ROAR_SOCKET_TYPE_UNKNOWN, server, port)) == -1 ) {
236      ROAR_ERR("Can not open listen socket!");
237      return 1;
238     }
239    }
240   } else {
241    ROAR_ERR("Can not open listen socket!");
242    return 1;
243   }
244  }
245
246  if ( *server == '/' ) {
247   if ( sock_user ) {
248    if ( (pwd = getpwnam(sock_user)) == NULL ) {
249     ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
250    }
251   }
252   if ( (grp = getgrnam(sock_grp)) == NULL ) {
253    ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
254   } else {
255    if ( pwd ) {
256     chown(server, pwd->pw_uid, grp->gr_gid);
257    } else {
258     chown(server, -1, grp->gr_gid);
259    }
260    if ( getuid() == 0 )
261     chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
262   }
263  }
264 }
265
266 if ( output_buffer_init(&sa) == -1 ) {
267  ROAR_ERR("Can not init output buffer!");
268  return 1;
269 }
270
271 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
272  ROAR_ERR("Can not open output driver!");
273  return 1;
274 }
275
276 if ( samples_init() == -1 ) {
277  ROAR_ERR("Can not init samples!");
278  return 1;
279 }
280
281
282 signal(SIGINT,  on_sig_int);
283 signal(SIGCHLD, on_sig_chld);
284 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
285
286 if ( realtime ) {
287#ifdef DEBUG
288  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
289#endif
290
291  errno = 0;
292  nice(-5*realtime); // -5 for each --realtime
293  if ( errno )
294   ROAR_WARN("Can not decrease nice value by 5: %s", strerror(errno));
295/*
296#ifdef __linux__
297  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
298   ROAR_WARN("Can not set io priority: %s", strerror(errno));
299#endif
300*/
301 }
302
303 if ( setids & R_SETGID ) {
304  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
305   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
306  }
307  if ( setgid(grp->gr_gid) == -1 ) {
308   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
309  }
310 }
311
312
313 clients_set_pid(g_self_client, getpid());
314 clients_set_uid(g_self_client, getuid());
315 clients_set_gid(g_self_client, getgid());
316 clients_get(g_self_client, &self);
317
318 if ( self == NULL ) {
319  ROAR_ERR("Can not get self client!");
320  return 1;
321 }
322
323 strcpy(self->name, "RoarAudio demon internal");
324
325 if ( demon ) {
326  close(ROAR_STDIN );
327  close(ROAR_STDOUT);
328  close(ROAR_STDERR);
329  setsid();
330  if ( fork() )
331   _exit(0);
332 }
333
334 if (chrootdir) {
335  if ( chroot(chrootdir) == -1 ) {
336   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
337   return 2;
338  }
339  if ( chdir("/") == -1 ) {
340   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
341   return 2;
342  }
343 }
344
345 if ( setids & R_SETUID ) {
346  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
347   ROAR_ERR("Can not set UserID: %s", strerror(errno));
348   return 3;
349  }
350  clients_set_uid(g_self_client, getuid());
351 }
352
353 // start main loop...
354 main_loop(drvid, drvinst, &sa);
355
356 // clean up.
357 clean_quit_prep();
358 driver_close(drvinst, drvid);
359 output_buffer_free();
360
361 return 0;
362}
363
364void clean_quit_prep (void) {
365 close(g_listen_socket);
366
367 if ( *server == '/' )
368  unlink(server);
369
370
371 sources_free();
372 streams_free();
373 clients_free();
374 midi_cb_stop(); // stop console beep
375 midi_free();
376}
377
378void clean_quit (void) {
379 clean_quit_prep();
380// driver_close(drvinst, drvid);
381// output_buffer_free();
382 exit(0);
383}
384
385//ll
Note: See TracBrowser for help on using the repository browser.