source: eviacam/trunk/fuentes/src/eviacamloader.c.in @ 29

Last change on this file since 29 was 29, checked in by mabarracus, 4 years ago

added eviacam content

  • Property svn:executable set to *
File size: 3.8 KB
Line 
1/////////////////////////////////////////////////////////////////////////////
2// Name:        eviacamloader.c
3// Purpose: 
4// Author:      Cesar Mauri Loba (cesar at crea-si dot com)
5// Modified by:
6// Created:     09/06/2010
7// Copyright:   (C) 2010 Cesar Mauri Loba - CREA Software Systems
8//
9//  This program is free software: you can redistribute it and/or modify
10//  it under the terms of the GNU General Public License as published by
11//  the Free Software Foundation, either version 3 of the License, or
12//  (at your option) any later version.
13//
14//  This program is distributed in the hope that it will be useful,
15//  but WITHOUT ANY WARRANTY; without even the implied warranty of
16//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17//  GNU General Public License for more details.
18//
19//  You should have received a copy of the GNU General Public License
20//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21/////////////////////////////////////////////////////////////////////////////
22#include <stdio.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <unistd.h>
26#include <signal.h>
27#include <sys/stat.h>
28
29/* check wait status to see if child process finished abnormaly */
30int wait_status_correct (int status)
31{
32        /* Doesn't take into account an abnormal exit status. Usually it means that eviacam has
33           finished due to a fatal error (e.g. camera not found or cancelled by the user) */
34        /*     
35        if (WEXITSTATUS(status)!= 0) {
36                fprintf (stderr, "process finished abnormally. exit status: %d\n", WEXITSTATUS(status));
37                return 0;
38        }*/
39        if (WIFSIGNALED(status)) {
40                if (WCOREDUMP(status)) {
41                        fprintf (stderr, "process finished abnormally. core dumped\n");
42                        return 0;
43                }
44
45                int signum= WTERMSIG(status);
46                if (signum== SIGSEGV) {
47                        fprintf (stderr, "process finished abnormally. segmentation fault\n");
48                        return 0;
49                }
50                if (signum== SIGBUS) {
51                        fprintf (stderr, "process finished abnormally. bus error\n");
52                        return 0;
53                }
54        }
55        return 1;
56}
57
58int main (int argc, char **argv)
59{
60
61#ifndef NDEBUG
62        printf ("Use: %d, Group: %d. Eff. usr: %d, Eff. group: %d\n", getuid(), getgid(), geteuid(), getegid());
63#endif 
64
65        if (geteuid()== 0) {
66                /* running as root, change priority */
67                if (nice(-11)== -1) {
68                        perror("nice");
69                        return -1;
70                }
71
72                /* privileges revocation */
73                if (setgid (getgid())== -1 || setuid (getuid())== -1) {
74                        perror("setgid/setuid");
75                        return -1;
76                }
77        }
78        else
79                fprintf (stderr, "%s: this program needs root privileges. not renicing.\n", argv[0]);
80
81        /* run eviacam until normal exit */
82        for (;;) {
83                /* create child process to run eviacam */
84                pid_t pid= fork();
85                if (pid== -1) {
86                        perror ("fork");
87                        return -1;
88                }
89                else if (pid== 0) {
90                        /* child process */
91                       
92                        /*
93                                workaround to enable sound on systems with pulseaudio
94                                see [1] for more details
95                                [1] https://sourceforge.net/tracker/?func=detail&aid=3095561&group_id=248049&atid=1199422
96                                TODO: remove when this gets fixed
97                        */
98                               
99                        /* check is padsp command can be run */
100                        int padsp_command_available= 0;
101                        struct stat stat_buf;
102                        if (!stat("/usr/bin/padsp", &stat_buf)) padsp_command_available++;
103                       
104                        if (padsp_command_available) {
105                                if (execl ("/usr/bin/padsp", "eviacam", "@prefix@/bin/eviacam", NULL)== -1) {
106                                        perror("execl");
107                                        return -1;
108                                }
109                        }
110                        else {
111                                if (execl ("@prefix@/bin/eviacam", "eviacam", NULL)== -1) {
112                                        perror("execl");
113                                        return -1;
114                                }
115                        }
116                }
117                else
118                {
119                        /* parent process */
120                        int status;
121
122                        if (wait(&status)== -1) {
123                                perror ("wait");
124                                return -1;
125                        }
126
127                        /* check whether the child process finished abnormaly */
128                        if (!wait_status_correct(status)) {
129                                fprintf (stderr, "%s: abnormal process termination, restarting\n", argv[0]);
130                                sleep (1);
131                        }
132                        else    break;
133                }
134
135        }
136
137#ifndef NDEBUG
138        printf ("Use: %d, Group: %d. Eff. usr: %d, Eff. group: %d\n", getuid(), getgid(), geteuid(), getegid());
139#endif 
140        return 0;
141}
142
Note: See TracBrowser for help on using the repository browser.