I have a C code..
i which I have following code for UNIX:
l_iRet = system( "/bin/cp -p g_acOutputLogName g_acOutPutFilePath");
when I am running the binary generated..I am getting the following error:
cp: cannot access g_acOutputLogName
Can any one help me out?
You should generally prefer the exec family of functions over the system function. The system function passes the command to the shell which means that you need to worry about command injection and accidental parameter expansion. The way to call a subprocess using exec is as follows:
pid_t child;
child = fork();
if (child == -1) {
perror("Could not fork");
exit(EXIT_FAILURE);
} else if (child == 0) {
execlp("/bin/cp", g_acOutputLogName, g_acOutPutFilePath, NULL);
perror("Could not exec");
exit(EXIT_FAILURE);
} else {
int childstatus;
if (waitpid(child, &childstatus, 0) == -1) {
perror("Wait failed");
}
if (!(WIFEXITED(childstatus) && WEXITSTATUS(childstatus) == EXIT_SUCCESS)) {
printf("Copy failed\n");
}
}
Presumably g_acOutputLogName and g_acOutPutFilePath are char[] (or char*) variables in your program, rather than the actual paths involved.
You need to use the values stored therein, rather than the variable names, for example:
char command[512];
snprintf( command, sizeof command, "/bin/cp -p %s %s",
g_acOutputLogName, g_acOutPutFilePath );
l_iRet = system( command );
Related
I'm trying out FUSE for the first time by following the official GitHub Repo's example.
I have done the following:
created a mount directory called mount_dir that contains file hello.txt.
update /etc/fuse.conf with user_allow_other as mentioned in various forums and posts
Added bunch of printf() statements in hello_ll.c at all function entry points.
Executed ./hello_ll -o allow_other -f /home/hemalkumar/mount_dir
Executed ./test_stat. It calculate the number of pages in the file. Just some business logic, nothing fancy!
test_stat.c
#define PAGE_SIZE 4096
int main() {
char* filename = "/home/hemalkumar/mount_dir/hello.txt";
int fd = open(filename, O_RDONLY);
if (fd == -1) {
printf("INVALID file:%s\n", filename);
close(fd);
return -1;
}
struct stat sb;
if (fstat(fd, &sb) == -1) {
close(fd);
return -1;
}
int pages = sb.st_size/PAGE_SIZE + (sb.st_size % PAGE_SIZE != 0);
printf("pages:%d\n", pages);
return 0;
}
Issue:
When I execute test_stat without mounting FUSE, it works fine. However, running it after step#4 shows an error INVALID file:/home/hemalkumar/mount_dir/hello.txt.
I have updated /etc/fuse.conf file to allow other user, and passing flags during startup of hello_ll. Don't know what permission issues it is having.
Any pointers will be appreciated!
Thanks!
I'm looking for a preferably cross-platform way to detect from within a Tcl script if the interpreter is running in a foreground or in a background process.
I've seen how to do it via ps (or /proc/$$/stat on Linux); is there a better way or do I have to hack something around that approach? I already have a utility library written in C so exposing the lowlevel API that ps also uses so I don't have to parse process output (or special file content) would be fine.
There's no truly cross-platform notion of foreground, but the main platforms do have ways of doing it according to the notion they have of foreground.
Linux, macOS, and other Unix:
For determining if a process is foreground or not, you need to check if its process group ID is the terminal's controlling process group ID. For Tcl, you'd be looking to surface the getpgrp() and tcgetpgrp() system calls (both POSIX). Tcl has no built-in exposure of either, so you're talking either a compiled extension (may I recommend Critcl for this?) or calling an external program like ps. Fortunately, if you use the latter (a reasonable option if this is just an occasional operation) you can typically condition the output so that you get just the information you want and need to do next to no parsing.
# Tested on macOS, but may work on other platforms
proc isForeground {{pid 0}} {
try {
lassign [exec ps -p [expr {$pid ? $pid : [pid]}] -o "pgid=,tpgid="] pgid tpgid
} on error {} {
return -code error "no such process"
}
# If tpgid is zero, the process is a daemon of some kind
expr {$pgid == $tpgid && $tpgid != 0}
}
Windows
There's code to do it, and the required calls are supported by the TWAPI extension so you don't need to make your own. (WARNING! I've not tested this!)
package require twapi_ui
proc isForeground {{pid 0}} {
set forground_pid [get_window_thread [get_foreground_window]]
return [expr {($pid ? $pid : [pid]) == $foreground_pid}]
}
Thanks to Donal I came up with the implementation below that should work on all POSIX Unix variants:
/*
processIsForeground
synopsis: processIsForeground
Returns true if the process is running in the foreground or false
if in the background.
*/
int IsProcessForegroundCmd(ClientData clientData UNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
/* Check the arg count */
if (objc != 1) {
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
int fd;
errno = 0;
if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
const pid_t pgrp = getpgrp();
const pid_t tcpgrp = tcgetpgrp(fd);
if (pgrp != -1 && tcpgrp != -1) {
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(pgrp == tcpgrp));
close(fd);
return TCL_OK;
}
close(fd);
}
Tcl_SetErrno(errno);
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "processIsForeground: ", (char *)Tcl_PosixError(interp), NULL);
return TCL_ERROR;
}
int Pextlib_Init(Tcl_Interp *interp)
{
if (Tcl_InitStubs(interp, "8.4", 0) == NULL)
return TCL_ERROR;
// SNIP
Tcl_CreateObjCommand(interp, "processIsForeground", IsProcessForegroundCmd, NULL, NULL);
if (Tcl_PkgProvide(interp, "Pextlib", "1.0") != TCL_OK)
return TCL_ERROR;
return TCL_OK;
}
i was trying to make word that was detected to execute file.py.
here the source code that i try to modify at continuous.c
if (!in_speech && utt_started) {
/* speech -> silence transition, time to start new utterance */
ps_end_utt(ps);
hyp = ps_get_hyp(ps, NULL );
if (hyp = "OPEN"){
fopen("/home/pi/project/open.py", "r");
}
if (hyp != NULL) {
printf("%s\n", hyp);
fflush(stdout);
}
the program still detect the word but it still not execute the program that i want.
and here is the command that i was using
pocketsphinx_continuous -lm /home/pi/project/3379.lm -dict /home/pi/project/3379.dic -samprate 16000/8000/48000 -inmic yes -adcdev plughw:1,0
really need help here. thanks before.
In C strings are compared with strcmp, not with =, with = you just assign pointers, not even compare them.
It should be
if (strcmp(hyp, "OPEN") == 0) {
....
}
I was managed to execute an external task on a different process (child proc) using the method AuthorizationExecuteWithPrivileges in the following manner :
AuthorizationExecuteWithPrivileges(
AuthorizationRef,
commandFullPath,
Flags,
Arguments,
&CommunicationsPipe)
However, I'd also like obtain the return value from that specific command. in bash it's saved in the special character $?. is there any equivalent in objective C ?
You can check for sub-process pid using the communication file you supplied to AuthorisationExecuteWithPrivileges in the following command :
FILE* pipe = NULL;
int status = AuthorizationExecuteWithPrivileges(auth, path, flags, args, &pipe)
if (status == errAuthorizationSuccess)
pid_t pid = fcntl(fileno(pipe), F_GETOWN, 0);
Then you can use the pid and wait till the sub-process returns and acquire its return value :
pid_t pid2 = 0;
while ((pid = waitpid(pid, &stat, WNOHANG)) == 0) { }
int terminationStatus = WEXITSTATUS(stat);
I have looked all over and I cannot seem to figure out how to do this.
I have a parent process that has created a pipe()
Now, I want to fork() the parent and then execlp() and pass the pipe() to the new program as a command line argument.
Then from inside the new program I need to be able to read the pipefd.
I've seen a bunch of stuff on how to do it from inside the same process, but nothing on how to do it like this.
Edit: Initial post is/was rather vague.
What I have so far is:
int pfd[2];
if(pipe(pfd) == -1) {
perror("Creating pipe\n");
exit(1);
}
pid_t pid = fork();
if(pid == -1) {
fprintf (stderr, "Initiator Error Message : fork failed\n");
return -1;
}
else if(pid == 0) { // child process
close(pipe0[1]); // close(write);
execlp("program", "program", pipe0[0], NULL);
}
but then I don't really understand what I should do from inside "program" to get the FD. I tried assigning it to all sorts of things, but they all seem to error.
Thank you in advance!
The forked and execed child automatically inherit the open pipe descriptors and the pipe output is usually fed as standard input so that a command line argument to find the pipe is pretty redundant:
if(!pipe(&pipefd))
switch(fork()) {
case 0: !dup2(pipefd[0],0)&&
execlp("cat","cat","-n","/dev/fd/0",0);
case -1: return perror("fork");
default: write(pipefd[1],"OK\n",3);
}