snk
snk

Reputation: 185

How to run makefile with libssh?

I have a problem with libssh (libssh.org). I need to run a makefile on the remote server. I do it with a command "channel_request_exec":

int SSHExecCmd (void(* MessSender)(char* CurMessage, bool IsError, CWnd* MainWnd),ssh_session session,  CString & ShellEcho, char * cmd, CWnd* MainWnd)
{
    ssh_channel channel;
    int rc;
    channel = ssh_channel_new(session);
    if (channel == NULL) return SSH_ERROR;
    rc = ssh_channel_open_session(channel);
    if (rc != SSH_OK)
    {
        ssh_channel_free(channel);
        return rc;
    }
    rc = ssh_channel_request_exec(channel, cmd);
    if (rc != SSH_OK)
    {
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return rc;
    }
    char buffer[256];
    unsigned int nbytes;
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    while (nbytes > 0)
    {
        if (fwrite(buffer, 1, nbytes, stdout) != nbytes)
        {
            ssh_channel_close(channel);
            ssh_channel_free(channel);
            return SSH_ERROR;
        }
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    }
    if (nbytes < 0)
    {
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return SSH_ERROR;
    }
    return SSH_OK;
}

Makefile is located in root:

all: mpi_cuda.o pattern2d.o
        mpicc mpi_cuda.o pattern2d.o -o mpi_cuda -lrt -lpthread -L/opt/cuda/lib64 -lcudart -lm

mpi_cuda.o: mpi_cuda.c
        mpicc -g -std=c99 -D_GNU_SOURCE -I/opt/cuda/include -c $< -o $@

pattern2d.o: pattern2d.cu
        nvcc -g -c $< -o $@

I send a command "make" and receive echo:

mpicc -g -std=c99 -D_GNU_SOURCE -I/opt/cuda/include -c mpi_cuda.c -o mpi_cuda.oda

but nothing happens (compilation is not performed).

If I do make with a putty: everything work. Echo:

make
mpicc -g -std=c99 -D_GNU_SOURCE -I/opt/cuda/include -c mpi_cuda.c -o mpi_cuda.o
mpi_cuda.c: В функции ‘main’:
mpi_cuda.c:148: предупреждение: недостаточно аргументов для указанного формата
nvcc -g -c pattern2d.cu -o pattern2d.o
mpicc mpi_cuda.o pattern2d.o -o mpi_cuda -lrt -lpthread -L/opt/cuda/lib64 -lcudart -lm

How do I solve this?

Upvotes: 2

Views: 957

Answers (2)

hyde
hyde

Reputation: 62898

Not familiar with libssh, but error could be, because environment gets set up differently, so running make through shell explicitly could help.

Try changing the command (make?) to

bash -c make

If that does not work, try

bash -c "export > env.txt ; make > make_out.txt 2> make_err.txt"

Then check if those file appeared, and what they contain, that should give good hints.

If you have a working and a non-working case, then get these files from both cases, and compare them (eg. with diff -u).

And change bash to whatever shell you use (and in that case check if -c is right switch to give command string, and if export is right command to show environment), if you're not using bash.


Based on comments below: Difference in env.txt could be, because some of the environment variables only get set for interactive shells. For example, in my Ubuntu box, start of .bashrc has lines like this:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

Now if any of those needed environemnt variables are set in .bashrc after that line, and your ssh connection is non-interactive (without pseudo-tty), these don't get set.

If this is the case, move these env variable sets to ~/.profile, or in ~/.bashrc before such test as above. Also do man bash, and read stuff about initialization files (like that ~/.bashrc).

Other solution would be to make the ssh-session interactive, which I believe is documented at this page for libssh: http://api.libssh.org/master/libssh_tutor_shell.html .

Upvotes: 1

asn
asn

Reputation: 838

I would suggest to open a non-interactive shell and do it there. See

http://api.libssh.org/master/libssh_tutor_shell.html

Upvotes: 1

Related Questions