Hi,
I have reopened this thread for a new problem
.
I have learned how to call VLC program with many parameters from C code with a system() function. I can say that it works perfectly.
Now I'm trying to use fork() + execv() instead of system() function.
The code is the following:
Code: Select all
#define _POSIX_SOURCE /* for kill() */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <unistd.h> /* for fork, exec, kill */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for waitpid */
/*
* fork a child process, execute vlc, and return it's pid.
* returns -1 if fork failed.
*/
pid_t callVLC(char* outputImage, char* IP, char ports[2][10]) {
char* args[10];
args[0] = " -I rc ";
args[1] = outputImage;
args[2] = " --fake-duration 7200000 --sout '#transcode{vcodec=H263p,width=352,height=288,acodec=none}:duplicate{dst=rtp{dst=";
args[3] = IP;
args[4] = ",port-audio=";
args[5] = ports[0];
args[6] = ",port-video=";
args[7] = ports[1];
//args[8] = ",sdp=file:///home/tore/Scrivania/appconference_modificato/temp/file.sdp}}' vlc://quit &";
args[8] = "}}' ";
args[9] = NULL;
pid_t processID = fork();
if (processID == -1) {
perror("fork");
return -1;
}
if (processID == 0) {
//execvp("vlc", args);
execv("/usr/bin/vlc", args);
perror("vlc");
abort();
}
else {
/* parent, return the child's PID back to main. */
printf("execv vlc: PID %d\n", processID);
//ast_log(LOG_NOTICE, "execv vlc: PID %d\n", processID);
return processID;
}
}
int main() {
char * outputImage = "image.jpg";
char* IP = "192.168.0.4";
char ports[2][10];
pid_t processID = callVLC(outputImage, IP, ports);
if (processID == -1) {
fprintf(stderr, "failed to fork child process\n");
return 0;
}
printf("spawned vlc with pid %d\n", processID);
sleep(30);
/* kill will send the specified signal to the specified process.
* in this case, we send a TERM signal to VLC, requesting that it
* terminate. If that doesn't work, we send a KILL signal.
* If that doesn't work, we give up. */
if (kill(processID, SIGTERM) < 0) {
perror("kill with SIGTERM");
if (kill(processID, SIGKILL) < 0) {
perror("kill with SIGKILL");
}
}
/* this shows how we can get the exit status of our child process.
* it will wait for the the VLC process to exit, then grab it's return
* value. */
int status = 0;
waitpid(processID, &status, 0);
printf("VLC exited with status %d\n", WEXITSTATUS(status));
return 0;
}
With the function execv("usr/bin/vlc", args) the code works well while with execv("usr/bin/vlc-wrapper", args) doesn't work.
I need to use the code as root user. How can I solve the problem?
Thank you