--- embedtools/src/Attic/updimg.c 2009/11/14 00:59:14 1.1.2.5 +++ embedtools/src/Attic/updimg.c 2011/06/08 12:45:41 1.2 @@ -1,5 +1,14 @@ +/************************************************************************* + * (C) 2010 AITNET - Sofia/Bulgaria - + * by Michael Pounov + * + * $Author: misho $ + * $Id: updimg.c,v 1.2 2011/06/08 12:45:41 misho Exp $ + * + *************************************************************************/ #include "global.h" #include "upd.h" +#include "tftp.h" sl_config cfg; @@ -13,6 +22,7 @@ static void Usage() "=== %s === %s@%s ===\n\n" " Syntax: updimg [-v] [operate_dir]\n\n" "\t-v\t\tVerbose ...\n\n" + "\t-D\t\tRun tftp server and watch for updates (image_name==ip:port)\n" "\t-a\t\tMake image active for next boot\n" "\t-i \tInstall new image\n" "\t-r\t\tRollback old backuped image\n" @@ -26,11 +36,16 @@ static void Usage() int main(int argc, char **argv) { - char ch, szImg[MAXPATHLEN], szTFTP[MAXPATHLEN]; + char ch, *pos, szImg[MAXPATHLEN], szTFTP[MAXPATHLEN]; int mode; + struct hostent *host; + struct sockaddr_in sin; - while ((ch = getopt(argc, argv, "hvdbt:ai:r")) != -1) + while ((ch = getopt(argc, argv, "hvdbt:ai:rD")) != -1) switch (ch) { + case 'D': + Mode |= 0x40; + break; case 'a': Mode |= 0x1; break; @@ -87,15 +102,42 @@ int main(int argc, char **argv) Usage(); return 1; } else - basename_r(*argv, szImg); - if (argc > 1) { + strlcpy(szImg, basename(*argv), MAXPATHLEN); + if (0x40 & Mode) { + if (0x40 != Mode) { + printf("Error:: Daemon mode can`t be specified with others ...\n"); + return 1; + } + + pos = strchr(szImg, ':'); + if (!pos) + sin.sin_port = htons(DEFAULT_TFTP); + else { + *pos++ = 0; + sin.sin_port = htons(atoi(pos)); + if (!sin.sin_port) + sin.sin_port = htons(DEFAULT_TFTP); + } + host = gethostbyname(szImg); + if (!host) { + printf("Error:: in resolv host %s #%d - %s\n", szImg, h_errno, hstrerror(h_errno)); + return 1; + } else + memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length); + + strlcpy(szTFTP, !argv[1] ? DEFAULT_TFTPDIR : argv[1], MAXPATHLEN); chdir(argv[1]); - VERB(5) printf("Info(5):: Change to dir %s\n", argv[1]); - } + VERB(2) printf("Info(2):: Host %s Port %d in Dir %s\n", + inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), szTFTP); + } else + if (argc > 1) { + chdir(argv[1]); + VERB(5) printf("Info(5):: Change to dir %s\n", argv[1]); + } openlog("updimg", LOG_CONS, 0); - for (mode = 0x20; mode; mode >>= 1) + for (mode = 0x40; mode; mode >>= 1) switch (Mode & mode) { case 0x1: Activate(szImg); @@ -114,6 +156,9 @@ int main(int argc, char **argv) break; case 0x20: Rollback(szImg); + break; + case 0x40: + Daemonize(sin, szTFTP); break; }