USB Enable and Disable - usb

I have created an application which enables and disables the USB port through registry, but i am facing one issue if i plugged in the pen drive into my system and changed the registry value to 4 i.e disable the USB port, the value gets changed but for the inserted pen-drive i am able to do the operations. Now after changing the value to 4 (disabling), i re insert the pen-drive then its not detected.
My problem after changing the value to 4 (disable mode) when the pen drive is plugged in to the system I am able to use its functionality. Disable mode works only after re inserting the USB pen drive.

Right, the behavior makes perfect sense. You only disabled access after the drive was inserted.
Presumably, the system caches that registry value (like it does with most of the registry values) and only reads it when a new device is inserted. Since you have already inserted the device before changing the value, the system never has cause to read the new value and prevent you from accessing the drive.
This setting is not really intended to be used the way you're trying to use it. Most people disable access to USB devices on a system one time and then leave it that way. They don't use it to lock themselves out of a device that's currently attached—you can just unplug it.

Related

Raspberry Pi 3 (Raspbian) programatically accessing a USB drive: best way to do this?

I'm running Raspbian 1.0, v9/15 on an RP3. I'm working on an application that will access a dedicated USB flash drive to store collected data. The static path to write the data is "/media/user/kingston". This works great unless I have a system crash at which point the owner and group for "/media/user/kingston" are changed to root, and the mount point for the flash drive now is now changed to "/media/user/kingston1" which (clearly) does not work with the static path defined in my application. Is there a standard dynamic path option that is available for this device and OS? Thanks in advance...
Update: Based on BJ Black's advice, this is what I added to fstab after creating a directory named /usb at the root of the filesystem,
/dev/sda1 /usb vfat noatime,nofail,user,rw,exec,umask=000 0 3
The advantages are,
1) allows bootup even when no USB drive is inserted,
2) allows any usb storage device to be used here.
Disadvantage is that the system must be powered down (or drive umounted) before flash drive is removed otherwise bad things happen, (I tried this). Question: Is there anything that I can do to reduce the "bad things" in the event that someone pulls out the usb while it is still mounted? Thanks in advance.
In general, I tend to prefer using a hard mount point for this kind of thing. There are possibly some neat tricks in /sys (or by using a grep against the output of mount(1) or /proc/mounts), but they'll fail as soon as you change out your Kingston stick for some other vendor (yuck).
Have a look at this link to better understand what's automounting your usb stick and how to shut that mechanism off.
As for mounting in a permanent place, I'd try either:
Create a mount point (say /data) and put an entry in fstab(5) so it gets mounted automatically on boot.
Same as 1, but use the noauto and user and do an explicit call to mount(1) from your application, or...
Install autofs and put an entry in auto.misc to automount the device (probably /dev/sda1) on use. Then symlink /data -> /misc/data.
Of these, I like option 3 the best, as the stick will unmount automatically when idle. Then your application need only double-check that the path is writable beforehand.
Also be super-careful on writes, as cheapie USB sticks tend to erase in large blocks and their wear levelling is weak at best (so both small random writes and large writes tend to wear the flash fast). Using f2fs or careful write buffering helps (and the noatime option on mount helps). And don't call fsync() all the time in your app!

Creating a computer ID using Mono

I develop a few tools in C# using the .Net framework and I am considering the move to Mono. Based on the compatibility analyzer tool (MOMA), the only compatibility issue is the licensing code:
I use Window's serial number and a few other things to generate a computer ID which I then use to generate and manage my licenses.
Obviously, Windows will not be available on Linux or Mac, so I need another way to generate a computer ID.
What can I use in mono to create a reasonably reliable computer ID?
Use a MAC Address:
Get MAC Address in linux using mono
That should be unique to a given network interface, which would uniquely identify the machine.
Also, see this thread discussing the pitfalls and issues related to DRM in mono.
You can use a combination of either CPUID and a hard drive serial number.
As for your code, there is not going to be an elegant way to do this with a one-liner. You will have to check which platform you are running on via Environment.OSVersion. And then based on that, branch your code to do system-specific access. For example, to get CPUID/hdd serial number, use a WMI query; or parse the output of hdparm -I /dev/hda and go from there.
You could use a portion of how Microsoft checked hardware for Windows XP activation. They checked 10 items
Display Adapter
SCSI Adapter
IDE Adapter (effectively the motherboard)
Network Adapter (NIC) and its MAC Address
RAM Amount Range (i.e., 0-64mb, 64-128mb, etc.)
Processor Type
Processor Serial Number
Hard Drive Device
Hard Drive Volume Serial Number (VSN)
CD-ROM / CD-RW / DVD-ROM
When XP was (re)installed/activated it'd contact their servers and if 7 or more of the above items were unchanged it would activate without the need to speak with anyone.
I, personally, would recommend against using the MAC address. As you mentioned, it can be easily changed. Additionally, most machines have two or more network cards now (wired and wireless). It gets worse with virtualization as the MAC can be changed even more easily than the physical one can (do you license differently for virtual environments?)
Another option, instead of checking all 10 hardware components, is to use the UUID of the root file system. The downside of this, is that if the drive ever has issues, the license needs to be reacquired.
sudo blkid
/dev/sda1: TYPE="ntfs" UUID="72C0DE8EC0DE57C5" LABEL="windows"
/dev/sda2: UUID="30fcb748-ad1e-4228-af2f-951e8e7b56df" SEC_TYPE="ext2" TYPE="ext3"
/dev/sda5: TYPE="swap" UUID="8c4e69f8-5074-42c0-8134-0b2429c4c02c"
/dev/sdb1: SEC_TYPE="msdos" UUID="4848-E35A" TYPE="vfat"

In OSX (Mountain Lion) Only allowing an application to open if a condition is met

Is there any simple way to do this?
I basically want iTunes to not open if an external hard drive is not connected. This is essentially a user issue - as despite asking multiple times, my girlfriend will forget and open an audio or video file without the external HDD connected (where the iTunes library is kept) and so the usual rigamarole occurs... the media file will then try and add itself to the iTunes library, which can't be found, so it'll default back to it's position on the internal hard disk, and then when I come to use it, it'll try and consolidate it for me (which is nice), except it then decides it needs to organise it - and this takes about 6 hours due to the amount of music I have on there.
I've tried changing the internal (default) iTunes music folder path to an alias to the external one, but that starts throwing out some beastly errors once you get into the loop (when the alias is essentially a pointer to an invalid location).
Is Automator something that could be used? Sorry I'm not very pro with OS X I'm afraid.
Thanks,
Duncan
It seems a straightforward way to do this is just to move the iTunes.app onto the external hard drive, and replace iTunes.app in the Applications folder with an alias pointing to iTunes on the external HDD (called iTunes).
As iTunes is system protected, you'll need to change the permissions of iTunes.app to move it, which you can do via gui or terminal (sudo chmod...)
Simples.

How to force a version conflict in iCloud

I have a working implementation of iCloud. Now I want to improve conflict handling by adding some merge functionality. I've been trying to come up with a consistent way of forcing a conflict for testing purposes but I haven't had luck so far, conflicts don't occur consistently when I expect them to occur. This might indicate that I'm doing something wrong, or maybe that I just misunderstood something about how iCloud works (yet another thing, I mean).
I'm using UIDocument and yes, I'm listening to the UIDocumentStateChangedNotification. In fact, I do get some occasional conflict notifications. Also, I only have one file in iCloud.
Having two devices using the same iCloud accout, here is the flow of events I was expecting to always cause a conflict:
Open the file on both devices (both devices are now correctly seeing the same content). Note: Here is the only time openWithCompletionHandler is called, after this it's never called again.
Make some change on device A and call saveToURL.
Wait some time to allow the change to propagate.
Make some other change on device B and call saveToURL.
Wait some time to allow the change to propagate.
EXPECTED: The app should be getting a conflict notification from iCloud. OBSERVED: A conflict does occur very occasionally, but most of the time what happens is simply that the UIDocument gets its UIDocumentStateEditingDisabled flag set and then cleared back after half a second or so (I'd guess editing is being disabled while the iCloud daemon is pulling the version from the other device and saving it in the local ubiquitous directory).
Much like a version control system like SVN, I was expecting the version from device B to cause a conflict because an "update" was required in order to get the version uploaded by device A.
Am I wrong expecting a conflict in the scenario I just described? Why? Is there any other way to consistently force a conflict?
Thanks!
I would have thought a better way to cause a conflict would be to:
Make sure both devices have an up-to-date copy of the data
Put both devices into Airplane Mode to prevent any iCloud updates
Change the data in the same place on both devices, each with different new data
Turn the network back on
Wait for the changes to propagate
From the docs:
Conflicts occur when two instances of an app change a file locally and both changes are then transferred to iCloud. For example, this can happen when the changes are made while the device is in Airplane mode and cannot transmit changes to iCloud right away. When it does happen, iCloud stores both versions of the file and notifies the apps’ file presenters that a conflict has occurred and needs to be resolved.
The way you are doing it (allowing time for sync, altering the documents differently) seems like it shouldn't cause a conflict.
iCloud works basically the same as version control system - except that you can only access the conflict versions (when conflict happens).
When a device pulled ver_1 from iCloud, edit, save, and find the server has a different version (ver_2 or newer) than it expected, a conflicted version will be created.
After the initial sync, you can:
turn off wifi on device B, edit & save.
edit on device A, save.
turn on wifi on device B.
A conflict will come soon.

Remove autorun.inf from pendrive automatically

I've made a simple application to automatically remove autorun.inf from pendrive.
My algo is very simple, at an interval the app runs how many drive is there if it gets a Removable disk it tries to delete autorun.inf file.
But i want to optimize it by removing the timer.
How to do it ?
Your question isn’t to clear. I assume you mean you want your application to run every time a USB drive is inserted? You should be able to do it using a managementeventwatcher.
http://msdn.microsoft.com/en-us/library/system.management.managementeventwatcher.aspx
This component runs a query you provide on your computers WMI instance. WMI will detect your USB drive and then your watcher will raise the event.