* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
static char sccsid
[] = "@(#)docmd.c 5.1 (Berkeley) %G%";
#define RDIST "/usr/ucb/rdist"
FILE *lfp
; /* log file for recording files updated */
struct subcmd
*subcmds
; /* list of sub-commands for current cmd */
* Do the commands in cmds (initialized by yyparse).
docmds(dhosts
, argc
, argv
)
register struct namelist
*f
;
signal(SIGQUIT
, cleanup
);
signal(SIGTERM
, cleanup
);
for (c
= cmds
; c
!= NULL
; c
= c
->c_next
) {
if (dhosts
!= NULL
&& *dhosts
!= NULL
) {
for (cpp
= dhosts
; *cpp
; cpp
++)
if (strcmp(c
->c_name
, *cpp
) == 0)
for (cpp
= argv
; *cpp
; cpp
++) {
if (c
->c_label
!= NULL
&&
strcmp(c
->c_label
, *cpp
) == 0) {
for (f
= c
->c_files
; f
!= NULL
; f
= f
->n_next
)
if (strcmp(f
->n_name
, *cpp
) == 0)
doarrow(cpp
, c
->c_files
, c
->c_name
, c
->c_cmds
);
dodcolon(cpp
, c
->c_files
, c
->c_name
, c
->c_cmds
);
fatal("illegal command type %d\n", c
->c_type
);
* Process commands for sending files to other machines.
doarrow(filev
, files
, rhost
, cmds
)
register struct namelist
*f
;
register struct subcmd
*sc
;
int n
, ddir
, opts
= options
;
printf("doarrow(%x, %s, %x)\n", files
, rhost
, cmds
);
error("no files to be updated\n");
ddir
= files
->n_next
!= NULL
; /* destination is a directory */
printf("updating host %s\n", rhost
);
signal(SIGPIPE
, lostconn
);
if ((lfp
= fopen(tmpfile
, "w")) == NULL
) {
fatal("cannot open %s\n", tmpfile
);
for (f
= files
; f
!= NULL
; f
= f
->n_next
) {
for (cpp
= filev
; *cpp
; cpp
++)
if (strcmp(f
->n_name
, *cpp
) == 0)
for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
) {
if (sc
->sc_type
!= INSTALL
)
install(f
->n_name
, sc
->sc_name
,
sc
->sc_name
== NULL
? 0 : ddir
, sc
->sc_options
);
install(f
->n_name
, NULL
, 0, options
);
(void) signal(SIGPIPE
, cleanup
);
for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
)
if (sc
->sc_type
== NOTIFY
)
notify(tmpfile
, rhost
, sc
->sc_args
, 0);
for (; ihead
!= NULL
; ihead
= ihead
->nextp
) {
if ((opts
& IGNLNKS
) || ihead
->count
== 0)
log(lfp
, "%s: Warning: missing links\n",
* Create a connection to the rdist server on the machine rhost.
register char *ruser
, *cp
;
static char *cur_host
= NULL
;
printf("makeconn(%s)\n", rhost
);
if (cur_host
!= NULL
&& rem
>= 0) {
if (strcmp(cur_host
, rhost
) == 0)
strncpy(tuser
, rhost
, sizeof(tuser
)-1);
printf("updating host %s\n", rhost
);
(void) sprintf(buf
, "%s -Server%s", RDIST
, qflag
? " -q" : "");
if ((sp
= getservbyname("shell", "tcp")) == NULL
)
fatal("shell/tcp: unknown service");
printf("port = %d, luser = %s, ruser = %s\n", ntohs(port
), user
, ruser
);
printf("buf = %s\n", buf
);
rem
= rcmd(&rhost
, port
, user
, ruser
, buf
, 0);
if (read(rem
, cp
, 1) != 1)
if (read(rem
, cp
, 1) != 1)
} while (*cp
++ != '\n' && cp
< &buf
[BUFSIZ
]);
while (*cp
>= '0' && *cp
<= '9')
n
= (n
* 10) + (*cp
++ - '0');
if (*cp
== '\0' && n
== VERSION
)
error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION
, n
);
error("connection failed: version numbers don't match\n");
* Signal end of previous connection.
(void) write(rem
, "\2\n", 2);
log(lfp
, "rdist: lost connection\n");
register char *cp
= name
;
if (!isalpha(c
) && !isdigit(c
) && c
!= '_' && c
!= '-')
error("invalid user name %s\n", name
);
extern char target
[], *tp
;
* Process commands for comparing files to time stamp files.
dodcolon(filev
, files
, stamp
, cmds
)
register struct subcmd
*sc
;
register struct namelist
*f
;
error("no files to be updated\n");
if (stat(stamp
, &stb
) < 0) {
error("%s: %s\n", stamp
, sys_errlist
[errno
]);
printf("%s: %d\n", stamp
, stb
.st_mtime
);
if (nflag
|| (options
& VERIFY
))
if ((tfp
= fopen(tmpfile
, "w")) == NULL
) {
error("%s: %s\n", stamp
, sys_errlist
[errno
]);
(void) gettimeofday(&tv
[0], &tz
);
(void) utimes(stamp
, tv
);
for (f
= files
; f
!= NULL
; f
= f
->n_next
) {
for (cpp
= filev
; *cpp
; cpp
++)
if (strcmp(f
->n_name
, *cpp
) == 0)
for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
)
if (sc
->sc_type
== NOTIFY
)
notify(tmpfile
, NULL
, sc
->sc_args
, lastmod
);
if (!nflag
&& !(options
& VERIFY
))
* Compare the mtime of file to the list of time stamps.
printf("cmptime(%s)\n", name
);
printf("comparing dates: %s\n", name
);
* first time cmptime() is called?
if (exptilde(target
, name
) == NULL
)
if (access(name
, 4) < 0 || stat(name
, &stb
) < 0) {
error("%s: %s\n", name
, sys_errlist
[errno
]);
switch (stb
.st_mode
& S_IFMT
) {
error("%s: not a plain file\n", name
);
if (stb
.st_mtime
> lastmod
)
log(tfp
, "new: %s\n", name
);
register struct direct
*dp
;
printf("rcmptime(%x)\n", st
);
if ((d
= opendir(target
)) == NULL
) {
error("%s: %s\n", target
, sys_errlist
[errno
]);
while (dp
= readdir(d
)) {
if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
if (len
+ 1 + strlen(dp
->d_name
) >= BUFSIZ
- 1) {
error("%s/%s: Name too long\n", target
, dp
->d_name
);
* Notify the list of people the changes that were made.
* rhost == NULL if we are mailing a list of changes compared to at time
notify(file
, rhost
, to
, lmod
)
register struct namelist
*to
;
if ((options
& VERIFY
) || to
== NULL
)
if ((fd
= open(file
, 0)) < 0) {
error("%s: %s\n", file
, sys_errlist
[errno
]);
if (fstat(fd
, &stb
) < 0) {
error("%s: %s\n", file
, sys_errlist
[errno
]);
* Create a pipe to mailling program.
pf
= popen(MAILCMD
, "w");
error("notify: \"%s\" failed\n", MAILCMD
);
* Output the proper header information.
fprintf(pf
, "From: rdist (Remote distribution program)\n");
if (!any('@', to
->n_name
) && rhost
!= NULL
)
fprintf(pf
, " %s@%s", to
->n_name
, rhost
);
fprintf(pf
, " %s", to
->n_name
);
if (!any('@', to
->n_name
) && rhost
!= NULL
)
fprintf(pf
, ", %s@%s", to
->n_name
, rhost
);
fprintf(pf
, ", %s", to
->n_name
);
fprintf(pf
, "Subject: files updated by rdist from %s to %s\n",
fprintf(pf
, "Subject: files updated after %s\n", ctime(&lmod
));
while ((len
= read(fd
, buf
, BUFSIZ
)) > 0)
(void) fwrite(buf
, 1, len
, pf
);
* Return true if name is in the list.
register struct namelist
*nl
;
for (nl
= list
; nl
!= NULL
; nl
= nl
->n_next
)
if (!strcmp(file
, nl
->n_name
))
* Return TRUE if file is in the exception list.
register struct subcmd
*sc
;
register struct namelist
*nl
;
printf("except(%s)\n", file
);
for (sc
= subcmds
; sc
!= NULL
; sc
= sc
->sc_next
) {
if (sc
->sc_type
!= EXCEPT
&& sc
->sc_type
!= PATTERN
)
for (nl
= sc
->sc_args
; nl
!= NULL
; nl
= nl
->n_next
) {
if (sc
->sc_type
== EXCEPT
) {
if (!strcmp(file
, nl
->n_name
))