Skip to content

Commit

Permalink
1, Put some shared function into single file named "Functions.c";
Browse files Browse the repository at this point in the history
2, Add Bonding support, but only function structured, no content.
  • Loading branch information
root committed Mar 13, 2009
1 parent 7dc4d51 commit c82b533
Show file tree
Hide file tree
Showing 18 changed files with 1,510 additions and 1,185 deletions.
119 changes: 119 additions & 0 deletions BondingCmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <stdio.h>
#include <stdlib.h>

#include "BondingCmd.h"
#include "Protocol.h"
#include "Debuger.h"
#include "Functions.h"

extern int CallShell(const char *, const char *, char *);

static const char * s_return_value_file = "/tmp/return_value_bonding";
const char * s_bonding_script = "/usr/sbin/sanager/Bonding.pl";
const char * s_bonding_get_all_eth_ports= "GetAllEthPorts";
const char * s_bonding_get_all_bondings = "GetAllBondings";
const char * s_bonding_add_bonding = "AddBonding";
const char * s_bonding_mod_bonding = "ModBonding";
const char * s_bonding_del_bonding = "DelBonding";

int GetAllBondings(char ** reply, int * reply_len)
{
if(0 != CallShell(s_bonding_script, s_bonding_get_all_bondings, NULL))
{
return -1;
}

return GetLinedupResultsFromFile(s_return_value_file,
reply,
reply_len);
}

int AddBonding(char * msg_body, int msg_body_len)
{
return CallShell(s_bonding_script, s_bonding_add_bonding, NULL);;
}

int ModBonding(char * msg_body, int msg_body_len)
{
return CallShell(s_bonding_script, s_bonding_mod_bonding, NULL);;
}

int DelBonding(char * msg_body, int msg_body_len)
{
return CallShell(s_bonding_script, s_bonding_del_bonding, NULL);
}

int GetAllEthPorts(char ** reply, int * reply_len)
{
if(0 != CallShell(s_bonding_script, s_bonding_get_all_eth_ports, NULL))
{
return -1;
}

return GetLinedupResultsFromFile(s_return_value_file,
reply,
reply_len);
}

int BondingCmd(int sock_fd, char * oneframe, int len)
{
TRACE (10, ("### Enter BondingCmd ###"));

int ret = -1;
PPACKET_HDR hdr;
char * msg_body;
int msg_body_len;
char * reply = NULL;
int reply_len = 0;

hdr = (PPACKET_HDR)oneframe;

msg_body = oneframe + PACKET_HDR_LEN;
msg_body_len = len - PACKET_HDR_LEN;

switch(hdr->subtype)
{
case BD_GET_ALL_BONDINGS:
ret = GetAllBondings(&reply, &reply_len);
break;
case BD_ADD_BONDING:
ret = AddBonding(msg_body, msg_body_len);
break;
case BD_MOD_BONDING:
ret = ModBonding(msg_body, msg_body_len);
break;
case BD_DEL_BONDING:
ret = DelBonding(msg_body, msg_body_len);
break;
case BD_GET_ALL_ETH_PORTS:
ret = GetAllEthPorts(&reply, &reply_len);
break;
default:
break;
}

TRACE (10, ("### Leave BondingCmd ###"));

if (SendAppFrame(sock_fd,
MSG_TYPE_BONDING,
((PACKET_HDR *) oneframe)->subtype,
(unsigned short) ret,
reply,
reply_len) <= 0)
{
if(NULL != reply)
{
free(reply);
}

return -1;
}

/* 切记不要忘了释放动态分配的内存*/
if(NULL != reply)
{
free(reply);
}

return 0;
}
6 changes: 6 additions & 0 deletions BondingCmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __BONDINGCMD_H__
#define __BONDINGCMD_H__

int BondingCmd(int sock_fd, char * one_frame, int len);

#endif
186 changes: 186 additions & 0 deletions Functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "Protocol.h"
#include "Functions.h"


int SendAppFrame(int sock_fd,
unsigned char type,
unsigned char sub_type,
unsigned short retcode,
char * data,
unsigned char data_len)
{
printf("### Enter SendAppFrame ###\n");

char one_frame[MAX_FRAME_LEN];

if (-1 == sock_fd)
{
printf("sock_fd == -1\n");
return -1;
}


PACKET_HDR hdr;

hdr.len = PACKET_HDR_LEN + data_len;
hdr.type = type;
hdr.subtype = sub_type;
hdr.retcode = retcode;

memcpy (one_frame, &hdr, PACKET_HDR_LEN);
memcpy (one_frame + PACKET_HDR_LEN, data, data_len);

// now we change data_len's real meaning
data_len = PACKET_HDR_LEN + data_len;

if (data_len != my_send (sock_fd, one_frame, data_len))
{
perror ("send");
return -1;
}

printf("### Leave SendAppFrame ###\n");

return hdr.len;
}

#define MAX_SHELL_CMD_LEN 256
int CallShell(const char * shell_script_name,
const char * shell_func_name,
char * param_list)
{
int pid;
int cmd_len = 0;

if(NULL == shell_script_name
|| NULL == shell_func_name)
{
return -1;
}

cmd_len = strlen(shell_script_name) + strlen(shell_func_name);
if(NULL != param_list)
{
cmd_len += strlen(param_list);
}

if(cmd_len >= MAX_SHELL_CMD_LEN - 2)
{
printf("Shell command is two long to stored in stack memory\n");
return -1;
}

pid = fork();

if (pid < 0)
{
return - 1;
}
else if (pid == 0)
{
char * argument[4];
char command[MAX_SHELL_CMD_LEN];
extern char **environ;

if(NULL == param_list)
{
snprintf(command,
MAX_SHELL_CMD_LEN,
"%s %s",
shell_script_name,
shell_func_name);
}
else
{
snprintf(command,
MAX_SHELL_CMD_LEN,
"%s %s \"%s\"",
shell_script_name,
shell_func_name,
param_list);
}

argument[0] = "sh";
argument[1] = "-c";
argument[2] = command;
argument[3] = NULL;
printf("pl command: %s\n", command);
execve("/bin/bash", argument, environ);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);

return WEXITSTATUS(status);
}
}



int GetLinedupResultsFromFile(const char * filename,
char ** reply,
int * reply_len)
{
if(NULL == filename
|| NULL == reply
|| NULL == reply_len)
{
return -1;
}

// Get results from /tmp/file
FILE * file = NULL;
char response[NET_BUFFER_LEN + 1];
char line_buf[SHORT_BUFFER_LEN];
char line_len = 0;
int response_len = 0;

file = fopen(filename, "r+");

if(NULL == file)
{
return -1;
}

while(NULL != fgets(line_buf, SHORT_BUFFER_LEN, file))
{
if(0 == (line_len = strlen(line_buf)))
{
continue;
}

if(response_len + line_len > NET_BUFFER_LEN)
{
printf("NET_BUFFER_LEN(%d) not enough!\n", NET_BUFFER_LEN);
break;
}

memcpy(response + response_len, line_buf, line_len);
response_len += line_len;
response[response_len] = '\0';
response_len ++;
}

fclose(file);

(*reply) = (char *)malloc(response_len);
if(NULL == (*reply))
{
return -1;
}
// assert(NULL != (*reply));

memcpy(*reply, response, response_len);

*reply_len = response_len;

return 0;
}
36 changes: 36 additions & 0 deletions Functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef __FUNCTIONS_H__
#define __FUNCTIONS_H__


/*
* Called by applications, including ib,iscsi,bonding apps, to
* send back tcp packet to requestor on the peer.
*/
int SendAppFrame(int sock_fd,
unsigned char type,
unsigned char sub_type,
unsigned short retcode,
char * data,
unsigned char data_len);

/*
* Execute shell or perl scripts named by 'shell_script_name',
* using 'execve()'.
*/
int CallShell(const char * shell_script_name,
const char * shell_func_name,
char * param_list);

/*
* Get shell or perl scripts' output from memory filesystem.
* Each line in file will be treated as one record to specific
* object.
* Records will be delimited by C type ending charactor '\0'.
* All records will be combined into one 'BIG' buffer referenced
* by 'reply', and 'reply_len' represents the buffer's real length.
*/
int GetLinedupResultsFromFile(const char * filename,
char ** reply,
int * reply_len);

#endif
Loading

0 comments on commit c82b533

Please sign in to comment.