Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a 'paste' command to write a local file to the terminal #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rohieb
Copy link
Contributor

@rohieb rohieb commented Feb 6, 2021

This command can be useful as replacement for the clipboard, e.g. when working remotely over SSH, and when file transfer to the target is not available otherwise.

Usage example (glados is my host machine, RPi3 is the target, non-printable key presses in angle brackets):

rohieb@glados:~ $ cat apparmor-profile
#include <tunables/global>
profile /usr/bin/rauc {
  #include <abstractions/base>
  
  capability sys_rawio,

  /usr/bin/rauc mr,
  /usr/bin/barebox-state cx,
  /usr/sbin/unsquashfs cx,
  /usr/bin/busybox.nosuid cx,
  /run/rauc/bundle/hook.sh Ux,
  /tmp/** rw,
  /mnt/updatebundle.rauc rk,
  /etc/rauc/* r,
  /proc/cmdline r, 
  /proc/*/fd/ r,
  /proc/*/mountinfo r,
  /run/rauc/** rw,
  /dev/mmcblk* rw,
  /sys/devices/**/mmcblk*/mmcblk*boot*/force_ro rw,
  /etc/ssl/openssl.cnf r,
}
  
rohieb@glados:~ $ microcom  -s 115200 /dev/ttyUSB0
root@RPi3:~ cat /usr/share/apparmor/usr.bin.rauc
cat: can't open '/usr/share/apparmor/usr.bin.rauc': No such file or directory
root@RPi3:~ cat > /usr/share/apparmor/usr.bin.rauc
<Ctrl-\>
Enter command. Try 'help' for a list of builtin commands
-> paste apparmor-profile

----------------------
#include <tunables/global>
profile /usr/bin/rauc flags=(attach_disconnected) {
  #include <abstractions/base>
  
  capability sys_rawio,
           
  /usr/bin/rauc mr,
  /usr/bin/barebox-state cx,
  /usr/sbin/unsquashfs cx,
  /usr/bin/busybox.nosuid cx,
  /run/rauc/bundle/hook.sh Ux,
  /tmp/** rw,
  /mnt/updatebundle.rauc rk,
  /etc/rauc/* r,
  /proc/cmdline r, 
  /proc/*/fd/ r,
  /proc/*/mountinfo r,
  /run/rauc/** rw,
  /dev/mmcblk* rw,
  /sys/devices/**/mmcblk*/mmcblk*boot*/force_ro rw,
  /etc/ssl/openssl.cnf r,
}
<Ctrl-D>
root@RPi3:~ ls -l /usr/share/apparmor/usr.bin.rauc
-rw-r--r--    1 root     root           521 Sep  1 00:12 /usr/share/apparmor/usr.bin.rauc

This definition can be useful in other parts of the code too.

Signed-off-by: Roland Hieber <rhi@pengutronix.de>
Make clear that this is meant as batch processing for CLI commands
instead of executing e.g. an external shell script.

Signed-off-by: Roland Hieber <rhi@pengutronix.de>
This command can be useful as replacement for the clipboard, e.g. when working
remotely over SSH, and when file transfer to the target is not available
otherwise.

Usage example (glados is my host machine, RPi3 is the target, non-printable key
presses in angle brackets):

    rohieb@glados:~ $ cat apparmor-profile
    #include <tunables/global>
    profile /usr/bin/rauc {
      #include <abstractions/base>

      capability sys_rawio,

      /usr/bin/rauc mr,
      /usr/bin/barebox-state cx,
      /usr/sbin/unsquashfs cx,
      /usr/bin/busybox.nosuid cx,
      /run/rauc/bundle/hook.sh Ux,
      /tmp/** rw,
      /mnt/updatebundle.rauc rk,
      /etc/rauc/* r,
      /proc/cmdline r,
      /proc/*/fd/ r,
      /proc/*/mountinfo r,
      /run/rauc/** rw,
      /dev/mmcblk* rw,
      /sys/devices/**/mmcblk*/mmcblk*boot*/force_ro rw,
      /etc/ssl/openssl.cnf r,
    }

    rohieb@glados:~ $ microcom  -s 115200 /dev/ttyUSB0
    root@RPi3:~ cat /usr/share/apparmor/usr.bin.rauc
    cat: can't open '/usr/share/apparmor/usr.bin.rauc': No such file or directory
    root@RPi3:~ cat > /usr/share/apparmor/usr.bin.rauc
    <Ctrl-\>
    Enter command. Try 'help' for a list of builtin commands
    -> paste apparmor-profile

    ----------------------
    #include <tunables/global>
    profile /usr/bin/rauc flags=(attach_disconnected) {
      #include <abstractions/base>

      capability sys_rawio,

      /usr/bin/rauc mr,
      /usr/bin/barebox-state cx,
      /usr/sbin/unsquashfs cx,
      /usr/bin/busybox.nosuid cx,
      /run/rauc/bundle/hook.sh Ux,
      /tmp/** rw,
      /mnt/updatebundle.rauc rk,
      /etc/rauc/* r,
      /proc/cmdline r,
      /proc/*/fd/ r,
      /proc/*/mountinfo r,
      /run/rauc/** rw,
      /dev/mmcblk* rw,
      /sys/devices/**/mmcblk*/mmcblk*boot*/force_ro rw,
      /etc/ssl/openssl.cnf r,
    }
    <Ctrl-D>
    root@RPi3:~ ls -l /usr/share/apparmor/usr.bin.rauc
    -rw-r--r--    1 root     root           521 Sep  1 00:12 /usr/share/apparmor/usr.bin.rauc

Signed-off-by: Roland Hieber <rhi@pengutronix.de>
@rohieb rohieb changed the title add a paste command to write a local file to the terminal add a 'paste' command to write a local file to the terminal Feb 6, 2021
@rohieb
Copy link
Contributor Author

rohieb commented Oct 14, 2021

Does anyone want to review this? :-)

@@ -145,6 +145,38 @@ static int cmd_break(int argc, char *argv[])
return MICROCOM_CMD_START;
}

static int cmd_paste(int argc, char *argv[])
{
int fd = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to initialize.

static int cmd_paste(int argc, char *argv[])
{
int fd = -1;
int i = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leave uninitialized and see below.

return -ret;
}

while (i > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move check after initializing i with proper value.

if (i < 0) {
ret = errno;
fprintf(stderr, "%s\n", strerror(ret));
return -ret;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaks fd.

return -ret;
}

ios->write(ios, buf, i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You call write with i=0 here on last iteration. Is that intended? Otherwise move check between read and write. also consider early exit here on failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants